Zahy
Zahy

Reputation: 377

*** glibc detected *** realloc(): invalid old size

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

Upvotes: 0

Views: 2906

Answers (2)

tolya
tolya

Reputation: 11

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;

Upvotes: 1

sth
sth

Reputation: 229744

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

Upvotes: 4

Related Questions