【发布时间】:2013-10-25 10:39:20
【问题描述】:
我的程序因以下几行而崩溃:
警告:HEAP[maze.exe]: 警告:00392F30 处的堆块在 00392F3B 处修改,超过请求的 3 大小
我正在为字符串动态分配空间
int userReq() {
char **maze=NULL;
char *pchar;
int i, test_cases, cur_test=0;
int row, col;
/* gather the amount of test cases */
scanf("%d", &test_cases);
do{
scanf("%d",&row);
scanf("%d",&col);
/* allocate memory for char pointer row-wise */
maze = (char **) malloc(row*sizeof(char*));
for(i=0;i<row;i++)
/* for each cell allocate the num of chars in cell */
maze[i] = (char *) malloc(col*sizeof(char));
for(i=0;i<row;i++)
scanf("%s",maze[i]);
/* this function does modify the maze by changing some of the spots to a different char */
CallSomeFunctionHere(maze);
/* free first the cells then the entire block */
for(i=0;i<row;i++)
free(maze[i]);
free(maze);
cur_test = cur_test + 1;
}while(cur_test < test_cases);
/* if we were successful then exit program with
success */
return 0;
}
我的程序在执行逻辑然后尝试释放内存后崩溃。
【问题讨论】:
-
在 C 中,不要强制转换 malloc:stackoverflow.com/questions/605845/…
-
你的意思是你想释放的时候就崩溃了对吧?
-
您确定问题不在
CallSomeFunctionHere内部吗?另请注意,CallSomeFunctionHere在循环之外,这是您想要的吗?col到底是什么意思?最大限度。字符串长度?如果是这样,您忘记了空终止符的空间。 -
我建议使用
valgrind并花一些时间使用它
标签: c