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

发布于 2024-12-25 01:15:07 字数 1189 浏览 3 评论 0原文

realloc() 的问题。 我明白了 * glibc detectors * realloc(): invalid old size

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

Problems with realloc().
I get
* glibc detected * realloc(): invalid old size

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);
   }
}

I made sure I am not reallocating null or wrong pointers. Initial mem-allocation
is done by 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遇到 2025-01-01 01:15:07

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

You probably accidentally overwrote mallocs internal book keeping data, for example through a buffer overflow. Generally this is known as "memory corruption".

粉红×色少女 2025-01-01 01:15:07

你不能这样写:

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;

You can't write like this:

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

you must declare temporary pointer like this:

mmUctNode **ptr; 

and then:

if(!(ptr = (mmUctNode**)realloc(nodeToReallocate->childPtrTable,sizeof(mmUctNode*)*newSize))){
    //.... free old objects code
    puts("Re-allocation failed");
    exit(-1);
}
else nodeToReallocate->childPtrTable = ptr;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文