我的程序崩潰,並顯示下列行:堆大小錯誤字符串
警告: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 :http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Barmar
你的意思是說,當你想釋放權利時你會崩潰? – ncm
你確定問題不在'CallSomeFunctionHere'裏面嗎?還要注意'CallSomeFunctionHere'在循環之外,這是你想要的嗎? 「col」究竟意味着什麼?最大。字符串長度?如果是這樣,你忘記了空終止符的空間。 –