这可能有点长,所以我很抱歉。考虑下面的代码(我在代码中留下了一些不相关的部分)。这段代码接收一个指向结构(BoardP theBoard)、x&y坐标和一个值的指针。目标是将值放在结构中找到的二维数组中。如果坐标越界,我必须增加表的大小,将旧数据复制到新数据,并将值放在其位置。这段代码在第一个调用中可以正常工作,但是在第二个调用中,它崩溃并写道:
*** glibc detected *** ./b: double free or corruption (top): 0x092ae138 *** 我找不到答案,希望你能帮忙。
下面是main()的调用
BoardP p = CreateNewBoard(10,10);
PutBoardSquare(p,10,5,'X');
PutBoardSquare(p,5,10,'O');
Boolean PutBoardSquare(BoardP theBoard, int X, int Y, char val) {
if (inBounds(X,Y,theBoard->_rows,theBoard->_cols)) {
theBoard->_board[X * theBoard->_cols + Y] = val;
return TRUE;
}
else {
int newRows = (X>=theBoard->_rows) ? (2*X) : theBoard->_rows;
int newCols = (Y>=theBoard->_cols) ? (2*Y) : theBoard->_cols;
BoardP newBoard = CreateNewBoard(newCols,newRows); //this creates a new Board with the new dimensions
if (newBoard == NULL) {
//ReportError(MEM_OUT);
return FALSE;
}
else {
copyData(theBoard,newBoard);
freeBoardArray(&theBoard->_board[0]); //free old array
theBoard->_board = newBoard->_board; //old array point to new array
FreeBoard(newBoard); //free the temp copy THIS CAUSES THE PROBLEM
PutBoardSquare(theBoard,X,Y,val);//recursion, will be in bounds now
return TRUE;
}
}
}以下是Free函数:
void FreeBoard(BoardP board) {
if (board != NULL) {
printf("FREE 1\n");
//free the board array:
if (board->_board != NULL) {
printf("FREE 2\n");
freeBoardArray(&board->_board[0]);
printf("FREE 3\n");
}
free(board);
}
}
static void freeBoardArray(char * arrP) {
free(arrP); //**PROGRAM CRASH HERE**
}这就是我如何创建一个新的板子:
BoardP CreateNewBoard(int width, int high) {
BoardP board = (BoardP) malloc(sizeof(Board));
if (board != NULL) {
board->_board = allocateBoardArray(high,width);
if ( board->_board == NULL) {
FreeBoard(board);
//TODO make file ReportError(MEM_OUT);
return NULL;
}
initializeBoard(board,high,width,X_SIGN,SPACE);
return board;
}
else {
FreeBoard(board);
//TODO make file ReportError(MEM_OUT);
return NULL;
}
}
static char* allocateBoardArray(int row, int col) {
char* newBoard = (char*) malloc(row * col * sizeof(char));
if (newBoard == NULL) {
return NULL;
}
return newBoard;
}这是BoardP:
typedef struct Board* BoardP;发布于 2010-08-28 18:50:56
您必须释放已分配的内存,并且不再希望保留引用。从您的代码中,我可以看到下面这一行。
theBoard->_board = newBoard->_board;现在,您维护对已分配指针的引用,然后释放该指针本身。
示例代码:
char *foo()
{
char *ref1;
char *ref2;
ref1 = malloc(256);
ref2=ref1;// Holding reference to a pointer in another pointer
strcpy(ref1,"stackoverflow");
printf("%s %s",ref1,ref2); // This prints stackoverflow twice
free(ref1); // This is valid but you can access ref2 or ref1 after this point
return ref2; /// This will cause problems
}发布于 2010-08-28 19:02:08
试试这个:
copyData(theBoard, newBoard);
/* swap the _board pointers */
char * b = theBoard->_board;
theBoard->_board = newBoard->_board;
newBoard->_board = b;
FreeBoard(newBoard); /* cleanup the temp struct and the old array */发布于 2010-08-28 19:45:10
此错误说明您正在尝试释放已被您释放的内存。我在这里怀疑的是这段代码
if (board != NULL) {
printf("FREE 1\n");
//free the board array:
if (board->_board != NULL) {
printf("FREE 2\n");
freeBoardArray(&board->_board[0]);
printf("FREE 3\n");
}
free(board);一旦你释放了结构的一部分(&freeBoardArray->_ problem.Why );,然后你释放了整个结构(Board);,它看起来像是我导致了板子,你传递了_board指针的地址?我在同一行代码上写了代码,这会导致问题。
struct a{
int * next;};int main(){
struct a *aptr = (struct a *)malloc(sizeof(struct a));
aptr->next=(int *)malloc(5*sizeof(int));
free(&aptr->next);
free(aptr);
return 0;}
此代码将导致与您所显示的相同的问题。现在,在从free(&aptr->next)中删除'&‘后再次尝试此代码,;statement.It将正常工作。所以我想你得到了你需要修改的地方的线索。
https://stackoverflow.com/questions/3590423
复制相似问题