I'm trying to allocate a 2D char array to be accessed like ary[i][j]
, using this code:
#define stringmaxlen 20
void do_alloc( char ***vals, int valscount ){
*vals = (char**) calloc( sizeof( char** ), valscount );
int i = 0;
for ( ; i<valscount; i++ )
*vals[i] = (char*) calloc( sizeof( char* ), stringmaxlen );
}
int main( ){
//......
char** ary;
do_alloc( &ary, 10 );
strcpy( ary[0], "test" );
//......
}
Unfortunately, this causes an overflow somewhere and the program have a bug in execution, I got some references from here for the dynamic allocation: http://staff.science.nus.edu.sg/~phywjs/CZ1102/lecture20/sld014.htm.
I like to know what's wrong here and how to solve the issue, thanks.