Here's a pretty generic approach (can be used with any data type, really):
Code:
#include <stdio.h>
/*
It really isn't necessary to require a T***, but it makes calling the macro work
like a similar function call would in this context - a C template function, if you will.
*/
#define ALLOCATE_MATRIX( ptr, rows, cols )\
do\
{\
size_t\
idx = 0;\
*ptr = malloc( sizeof( **ptr ) * rows );\
while( idx < rows )\
{\
( *ptr )[ idx++ ] = malloc( sizeof( ***ptr ) * cols );\
}\
}\
while( 0 )
#define DEALLOCATE_MATRIX( ptr, rows, unused )\
do\
{\
size_t\
idx = 0;\
while( idx < rows )\
{\
free( ( *ptr )[ idx++ ] );\
}\
free( *ptr );\
*ptr = 0;\
}\
while( 0 )
int**
flags = 0;
int main( void )
{
ALLOCATE_MATRIX( &flags, 1024, 128 );
flags[ 511 ][ 63 ] = 574;
DEALLOCATE_MATRIX( &flags, 1024, 128 );
return 0;
}
hth.