5

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...

Sam
  • 1,842
  • 3
  • 19
  • 33
  • What is `macro->args` ? – cnicutar Apr 07 '13 at 13:00
  • @cnicutar arrrr aorry I corrected it. – Sam Apr 07 '13 at 13:02
  • don't cast the return of `malloc`. programming throught side effects with `+=`, argh. – Jens Gustedt Apr 07 '13 at 13:02
  • 4
    The problem is likely somewhere between the `malloc` and the `realloc`. – cnicutar Apr 07 '13 at 13:02
  • @sam You're probably overrunning the buffer somewhere along the way and corrupting the heap. You should try running your application under `valgrind`. – John Szakmeister Apr 07 '13 at 13:03
  • Sorry once again I had to correct it... `arg` is `char** arg` – Sam Apr 07 '13 at 13:04
  • 1
    The address that glibc detected curiously looks as if the high word of your address has been clashed. This is a typical "side effect" of casting the return of `malloc` on machines with 32 bit `int` and 64 bit pointers, when you try to quiecen your compiler after forgetting to include the prototype of `malloc`. – Jens Gustedt Apr 07 '13 at 13:04
  • @cnicutar Can you please tell what problem can it be? – Sam Apr 07 '13 at 13:14
  • @SAM Listen to Jens and jszakmeister. Start by including `stdlib.h` if you don't already have it. – cnicutar Apr 07 '13 at 13:16
  • @JensGustedt When you forget to include `stdlib.h` it's quite the same as forgetting `stdio.h` for `printf()` – Alex Apr 07 '13 at 13:16
  • I have included `stdlib.h` ... @JensGustedt I also tried removing the casting but no luck :( – Sam Apr 07 '13 at 13:19
  • @JensGustedt Yes I have 64-bit pointer and 32-bit `int`... – Sam Apr 07 '13 at 13:22
  • 1
    At any point in the code you conveniently left out, is `args` ever modified (not `*args`; **`args`**)? For example, `args++` – WhozCraig Apr 07 '13 at 13:25
  • @WhozCraig No I didn't modify `args`... Should I post the original function? – Sam Apr 07 '13 at 13:35
  • Well, here's a big clue. The above code **verbatim**, with proper inclusion of ``, compiles and runs correctly without error in stand-alone `main()`. If there is any *better* indication that the code you're leaving out with your `// ...` marker is the root of all-things-evil, you'll be hard pressed to find it. **Yes**. Post the original function in an update to your question (not here in a comment). – WhozCraig Apr 07 '13 at 13:35
  • 1
    My suggestion would be to step through the function in a debugger from the time the original `malloc()` is saved to `args` until you see the value stored in `args` change. That instruction is the one poking you in the eye. If you know how to use gdb to set a break-on-write data breakpoint, so much the better, but if not, just keep it simple and step through the code. And heed what everyone else here said. 1. `#include `, 2. Don't cast your malloc/realloc calls in C., 3. Use at least `-Wall -Werror` and *pay attention to warnings from your compiler.* – WhozCraig Apr 07 '13 at 13:44
  • @WhozCraig Your instructions were very helpful! Solved the problem as I have mentioned in my answer. Please make your comments the answer so that I can accept it. – Sam Apr 16 '13 at 10:23

1 Answers1

5

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!

Sam
  • 1,842
  • 3
  • 19
  • 33