*** glibc检测到*** realloc():旧的大小无效

时间:2012-01-04 18:41:14

标签: c glibc realloc

realloc()的问题。 我明白了 检测到 * glibc * realloc():旧尺寸无效

void reallocatePtrTable(mmUctNode* nodeToReallocate){
 int newSize = (nodeToReallocate->PtrTableCapacity)*INCREASE_FACTOR;
 printf("(re)Allocating %p with %d bytes. ",nodeToReallocate->childPtrTable,sizeof(mmUctNode*)*newSize);
 nodeToReallocate->childPtrTable=
     (mmUctNode**)realloc(nodeToReallocate->childPtrTable,sizeof(mmUctNode*)*newSize);
 printf(" Got %p\n",nodeToReallocate->childPtrTable);
if(!nodeToReallocate->childPtrTable){
    puts("Re-allocation failed");
    exit(-1);
   }
}

我确定我没有重新分配空或错误的指针。初始内存分配 由malloc()

完成
(re)Allocating 0x8801fc8 with 480 bytes.  Got 0x8807a98
(re)Allocating 0x8807a98 with 960 bytes.  Got 0x880d2b8
(re)Allocating 0x880d2b8 with 1920 bytes.  Got 0x8818290
(re)Allocating 0x8818290 with 3840 bytes.  Got 0x882e310
(re)Allocating 0x882e310 with 7680 bytes.  Got 0x885a410
(re)Allocating 0x885a410 with 15360 bytes.  Got 0x88b9018
(re)Allocating 0x88b9018 with 30720 bytes. *** glibc detected ***     /home/: realloc(): invalid old size: 0x088b9018 ***
Segmentation fault

2 个答案:

答案 0 :(得分:4)

您可能不小心覆盖了mallocs内部簿记数据,例如通过缓冲区溢出。通常这被称为“内存损坏”。

答案 1 :(得分:1)

你不能这样写:

nodeToReallocate->childPtrTable =
     (mmUctNode**)realloc(nodeToReallocate->childPtrTable,sizeof(mmUctNode*)*newSize);

你必须像这样声明临时指针:

mmUctNode **ptr; 

然后:

if(!(ptr = (mmUctNode**)realloc(nodeToReallocate->childPtrTable,sizeof(mmUctNode*)*newSize))){
    //.... free old objects code
    puts("Re-allocation failed");
    exit(-1);
}
else nodeToReallocate->childPtrTable = ptr;