realloc: invalid next size, detected by glibc

Sam picture Sam · Apr 7, 2013 · Viewed 9.4k times · Source

My code:

int args_size = 5;
char** args;

args = (char**) malloc(sizeof(char*) * args_size);

// ...

args = (char**) realloc(args, sizeof(char*) * (args_size += 5));

I want to increase the size by 5.

But I get this error:

*** glibc detected *** ./a.out: realloc(): invalid next size: 0x0000000000a971c0 ***

I know that a temp variable catching realloc is good, but just for simplicity...

Answer

Sam picture Sam · Apr 16, 2013

SOLVED

Initially, the size of args is 5 elements. As the program was filling args, it was mistakenly adding 6th element to it and then calling realloc.

That caused the error mentioned in the question.

Problem is solved by eliminating the error, by following the comments of WhozCraig, Jens Gustedt and others...

Thanks to all!