dynamic 2 dimensional array
-
Hi i know this is probably a stupid question but whats the proper syntax for for allocating memory for a 2d array? i tried int **array; array = new int[2][2] even tried the second line with *array this wont even compile so wahts the correct way to do this? thanks
-
Hi i know this is probably a stupid question but whats the proper syntax for for allocating memory for a 2d array? i tried int **array; array = new int[2][2] even tried the second line with *array this wont even compile so wahts the correct way to do this? thanks
The only way I know to do it is to create an array of int* and then assign each element to point to an array. e.g. int ** array = new int*[2]; for (int i = 0; i < 2; i++) { array[i] = new int[2]; }
-
Hi i know this is probably a stupid question but whats the proper syntax for for allocating memory for a 2d array? i tried int **array; array = new int[2][2] even tried the second line with *array this wont even compile so wahts the correct way to do this? thanks
Why don't you use a vector instead? They are much safer and easier to handle...
A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.
-
Why don't you use a vector instead? They are much safer and easier to handle...
A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.
... and also one dimensional.
-
... and also one dimensional.
vector < vector < int > > Matrix;
and now you have a 2d array... ;P
A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.
-
vector < vector < int > > Matrix;
and now you have a 2d array... ;P
A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.
I suppose that works, but ick. I guess if it does the job for you, though . . .
-
The only way I know to do it is to create an array of int* and then assign each element to point to an array. e.g. int ** array = new int*[2]; for (int i = 0; i < 2; i++) { array[i] = new int[2]; }
this is definitly the most silly way. for every new an entry in the heap will be created which will decrese the speed of your app dramatically if there are many fields for the array. Don't try it, just do it! ;-)
-
Hi i know this is probably a stupid question but whats the proper syntax for for allocating memory for a 2d array? i tried int **array; array = new int[2][2] even tried the second line with *array this wont even compile so wahts the correct way to do this? thanks
there is a much better and faster way!
int **array;
int i, j;array = new int[4]; 2*2=4
for( i = 0; i < 2; i++ )
{
for( j = 0; j < 2; j++ )
{
array[i*2+j] = i;
// ....
}
}this is the fastest way Don't try it, just do it! ;-)
-
there is a much better and faster way!
int **array;
int i, j;array = new int[4]; 2*2=4
for( i = 0; i < 2; i++ )
{
for( j = 0; j < 2; j++ )
{
array[i*2+j] = i;
// ....
}
}this is the fastest way Don't try it, just do it! ;-)
Alexander M. wrote: this is the fastest way Unless you have some metrics to support this, speed is probably going to be irrelevant. Your approach has a major flaw; namely that it is not intuitive. Read my response here as to why
array[i][j]
is much preferred overarray[i*2+j]
.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
this is definitly the most silly way. for every new an entry in the heap will be created which will decrese the speed of your app dramatically if there are many fields for the array. Don't try it, just do it! ;-)
Pft. Allocation of the array is not typically the source of speed overhead. Accessing array positions repeatedly is, and your way is no faster than mine. Mine is at least intuitive.
-
Hi i know this is probably a stupid question but whats the proper syntax for for allocating memory for a 2d array? i tried int **array; array = new int[2][2] even tried the second line with *array this wont even compile so wahts the correct way to do this? thanks
Here's what I do :
= calloc( ydim, yitmsize ); if( data[x] == NULL ) { for( i = 0; i < x; i++ ) // free all we have so far free( data[i] ); free( data ); return NULL; } } return data; } // a general 2d array release function void **Free2DArray( void **array, int xdim ) { int x; if( array == NULL ) return NULL; // sanity check for( x = xdim - 1; x >= 0; x-- ) { free( array[x] ); array[x] = NULL; } free( array ); return NULL; } // a specialized two-dimensional matrix allocator // this function handles all casting for us. float **AllocFloatMatrix( int xdim, int ydim ) { float **data = (float **)Alloc2DArray( xdim, sizeof( float * ), ydim, sizeof( float ) ); return data; } // a specialized two-dimensional matrix deallocator // this function handles all casting for us. float **FreeFloatMatrix( float **array, int xdim ) { Free2DArray( (void **)array, xdim ); return NULL; }to access data which I find that to be reasonably clean. __________________________________________ a two cent stamp short of going postal.