Im trying to reallocate a double char pointer dynamicly in a larger program and im always getting that error "realloc(): invalid next size", so i wrote a smaller test to show u guy my exact problem:
my little test:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void){
char **buf = malloc(sizeof(char*));
for(int i = 0; i < 20; i++){
buf[i] = malloc(sizeof(char) * 6);
strcpy(buf[i], "hallo");
printf("%s\n", buf[i]);
if( (realloc(buf, sizeof(char*) * (i+1))) == NULL) exit(-1);
}
return 0;
}
this is always crashing at the 4th reallocation with the full error message:
Error in `./myTest': realloc(): invalid next size: 0x0000000001cb2010 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f450a4a97e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x834aa)[0x7f450a4b54aa]
/lib/x86_64-linux-gnu/libc.so.6(realloc+0x179)[0x7f450a4b6839]
./myTest[0x40069c]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f450a452830]
./myTest[0x400529]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:01 663159
/home/qbongo/BS-PK-2017/10_pk/myTest
00600000-00601000 r--p 00000000 08:01 663159
/home/qbongo/BS-PK-2017/10_pk/myTest
00601000-00602000 rw-p 00001000 08:01 663159
/home/qbongo/BS-PK-2017/10_pk/myTest
01cb2000-01cd3000 rw-p 00000000 00:00 0 [heap]
7f4504000000-7f4504021000 rw-p 00000000 00:00 0
7f4504021000-7f4508000000 ---p 00000000 00:00 0
7f450a21c000-7f450a232000 r-xp 00000000 08:01 398530
/lib/x86_64-linux-gnu/libgcc_s.so.1
7f450a232000-7f450a431000 ---p 00016000 08:01 398530
/lib/x86_64-linux-gnu/libgcc_s.so.1
7f450a431000-7f450a432000 rw-p 00015000 08:01 398530
/lib/x86_64-linux-gnu/libgcc_s.so.1
7f450a432000-7f450a5f2000 r-xp 00000000 08:01 394742
/lib/x86_64-linux-gnu/libc-2.23.so
7f450a5f2000-7f450a7f2000 ---p 001c0000 08:01 394742
/lib/x86_64-linux-gnu/libc-2.23.so
7f450a7f2000-7f450a7f6000 r--p 001c0000 08:01 394742
/lib/x86_64-linux-gnu/libc-2.23.so
7f450a7f6000-7f450a7f8000 rw-p 001c4000 08:01 394742
/lib/x86_64-linux-gnu/libc-2.23.so
7f450a7f8000-7f450a7fc000 rw-p 00000000 00:00 0
7f450a7fc000-7f450a822000 r-xp 00000000 08:01 393312
/lib/x86_64-linux-gnu/ld-2.23.so
7f450aa07000-7f450aa0a000 rw-p 00000000 00:00 0
7f450aa20000-7f450aa21000 rw-p 00000000 00:00 0
7f450aa21000-7f450aa22000 r--p 00025000 08:01 393312
/lib/x86_64-linux-gnu/ld-2.23.so
7f450aa22000-7f450aa23000 rw-p 00026000 08:01 393312
/lib/x86_64-linux-gnu/ld-2.23.so
7f450aa23000-7f450aa24000 rw-p 00000000 00:00 0
7ffcc108f000-7ffcc10b0000 rw-p 00000000 00:00 0 [stack]
7ffcc1149000-7ffcc114b000 r--p 00000000 00:00 0 [vvar]
7ffcc114b000-7ffcc114d000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
[vsyscall]
Aborted (core dumped)