CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: new operator

  1. #1
    Join Date
    Apr 1999
    Posts
    45

    new operator

    How can I use 'new' to create a multi-dimensional array?

    Currently I have :

    /////////////////////////////
    char* myarray;
    myarray = new char[100][256];
    /////////////////////////////

    but this doesnt work! Am I doing something wrong, or will I have to settle for :

    myarray = new char[100*256];

    ???????
    Thanks,
    Sean.


  2. #2
    Guest

    Re: new operator

    Hi

    You can try to use templete of array in VC++
    My email: [email protected]

    Mr. Shengguo in China


  3. #3
    Join Date
    Apr 1999
    Posts
    383

    Re: new operator

    IMO the STL vector class is the easiest way to do this, and avoids messy explicit memory allocation and deallocation, for example:

    #include <iostream>
    #include <vector>
    using std::cout;
    using std::endl;
    using std::vector;

    typedef vector<char> Array1D;
    typedef vector<Array1D> Array2D;

    int main()
    {
    Array1D ar1(10, 'A'); // Array of 10 'A's
    Array2D array2D(10, ar1); // Array of 10 arrays of 'A's

    cout << "Before change array2D[5][5] = " << array2D[5][5] << endl;

    array2D[5][5] = 'B';

    cout << "After change array2D[5][5] = " << array2D[5][5] << endl;

    return 0;
    }

    Dave


  4. #4
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: new operator

    For a simple two-dimensional array of characters, you can use

    myarray = new char[100 * 256];

    You can refer to each individual character as:

    myarray[row * 256][column]

    (the '* 256' should be typed in literally) which is almost what you wanted. This is because myarray[row * 256] is an offset into the array of (row * 256) and adding the [column] further offsets that address by (column).

    Does this help?



    --
    Jason Teagle
    [email protected]

  5. #5
    Join Date
    Apr 1999
    Posts
    2

    Re: new operator

    I found the following in the Microsoft on-line documentation:

    When new is used to allocate a single object, it yields a pointer to that object; the resultant type is new-type-name * or type-name *. When new is used to allocate a singly dimensioned array of objects, it yields a pointer to the first element of the array, and the resultant type is new-type-name * or type-name *. When new is used to allocate a multidimensional array of objects, it yields a pointer to the first element of the array, and the resultant type preserves the size of all but the leftmost array dimension. For example:

    new float[10][25][10]

    yields type float (*)[25][10]. Therefore, the following code will not work because it attempts to assign a pointer to an array of float with the dimensions [25][10] to a pointer to type float:

    float *fp;
    fp = new float[10][25][10];

    The correct expression is:

    float (*cp)[25][10];
    cp = new float[10][25][10];

    The definition of cp allocates a pointer to an array of type float with dimensions [25][10] — it does not allocate an array of pointers.

    All but the leftmost array dimensions must be constant expressions that evaluate to positive values; the leftmost array dimension can be any expression that evaluates to a positive value. When allocating an array using the new operator, the first dimension can be zero — the new operator returns a unique pointer.

    The type-specifier-list cannot contain const, volatile, class declarations, or enumeration declarations. Therefore, the following expression is illegal:

    volatile char *vch = new volatile char[20];

    The new operator does not allocate reference types because they are not objects.




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured