R
raphfrk
While I am here, I just thought I'd ask if there is a recommended way
of handling 2d dynamic arrays.
I implement them using the following kind of code.
------------------------------------------------
int fname( int sizex, int sizey, <other inputs> )
{
int *(*bitmap);
int cntx;
bitmap = malloc( sizeof( *bitmap ) * sizex );
if( bitmap == NULL )
{
printf("Unable to allocate enough RAM in fname\n");
exit(0);
}
for( cntx=0;cntx<sizex;cntx++)
{
bitmap[cntx] = malloc( sizeof( *(bitmap[cntx]) ) * sizey );
if( bitmap[cntx] == NULL )
{
printf("Unable to allocate enough RAM in fname\n");
exit(0);
}
}
<code>
for( cntx=0;cntx<sizex;cntx++)
{
free(bitmap[cntx]);
}
free(bitmap);
}
-----------------------------------------
This seems to require that the following code be used to access each
element
(bitmap[xpos])[ypos]
Is there a neater way of doing it ?
of handling 2d dynamic arrays.
I implement them using the following kind of code.
------------------------------------------------
int fname( int sizex, int sizey, <other inputs> )
{
int *(*bitmap);
int cntx;
bitmap = malloc( sizeof( *bitmap ) * sizex );
if( bitmap == NULL )
{
printf("Unable to allocate enough RAM in fname\n");
exit(0);
}
for( cntx=0;cntx<sizex;cntx++)
{
bitmap[cntx] = malloc( sizeof( *(bitmap[cntx]) ) * sizey );
if( bitmap[cntx] == NULL )
{
printf("Unable to allocate enough RAM in fname\n");
exit(0);
}
}
<code>
for( cntx=0;cntx<sizex;cntx++)
{
free(bitmap[cntx]);
}
free(bitmap);
}
-----------------------------------------
This seems to require that the following code be used to access each
element
(bitmap[xpos])[ypos]
Is there a neater way of doing it ?