new and delete can be used to create arrays
Code:
int * anArray = NULL;
int ** a2dArray = NULL;
anArray = new int [10]; //the constant 10, can be replaced by a var.
if( anArray == NULL)
{
cout << "call to new failed!";
}
else
{
//now anArray points to 10 ints, so it is the same thing as an array
//anArray[0] thru anArray[9] are valid
}
delete [] anArray; // must remember to free the mem when done with it!!
That is to make a 1d array
2d is a little tricker
int size = 3;
a2dArray = new int * [size]; //notice i want to create 3 new pointers, these new pointers will be used to make 1d arrays, giving us a 2d array.
if (a2dArray == NULL)
{
cout << "call to new failed!";
}
else
{
for( int i=0 ; i<size ; i++)
{
a2dArray[i] = new int[size]; //creates 3 new ints
}
}
now a2dArray[0..3][0..3] are valid.
to free the 2d array
for( int i=0 ; i<size ; i++)
{
delete [] a2dArray[i];
}
delete [] a2dArray;