【发布时间】:2014-05-02 11:18:54
【问题描述】:
我正在尝试分配一个 2D 字符数组,以便像 ary[i][j] 一样访问,使用以下代码:
#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" );
//......
}
不幸的是,这会导致某处溢出,并且程序在执行中存在错误,我从这里获得了一些关于动态分配的参考:http://staff.science.nus.edu.sg/~phywjs/CZ1102/lecture20/sld014.htm。
我想知道这里出了什么问题以及如何解决这个问题,谢谢。
【问题讨论】:
标签: c arrays dynamic-allocation