Click to See Complete Forum and Search --> : new operator


Sean
April 16th, 1999, 01:21 AM
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.

April 16th, 1999, 02:43 AM
Hi

You can try to use templete of array in VC++
My email: shengguo@mail.sc.cninfo.net

Mr. Shengguo in China

Dave Lorde
April 16th, 1999, 07:20 AM
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

Jason Teagle
April 16th, 1999, 07:33 AM
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?

Martin Slaman
April 16th, 1999, 09:22 AM
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.