我想重新分配一个2D数组。我有一个counter
,itime
它会增加每一步。每一步,我都想重新分配我的数组,保留旧值,并向数组添加新值。 itime=1
时,我只使用malloc
,因为它是第一次分配。当itime
增加(e.q。itime=2
)时,会使用realloc
。在realloc进程中,我的GUI崩溃了。
有人可以告诉我崩溃的原因,我是否正确重新分配?
int itime;
char ** solpointer;
for( itime = 1 ; itime <= 4 ; itime++ ) {
if( itime == 1 ) {
solpointer = (char**)malloc(sizeof(char*) * itime);
solpointer[itime-1] = (char*)malloc(sizeof(char) * 32);
}
else {
solpointer = (char**)realloc(solpointer, sizeof(char*) * itime);
solpointer[itime-1] = (char*)malloc(sizeof(char) * 32 );
}
}