Reputation: 768
So I'm trying to reallocate a 2d char array and it keeps failing
I have stdlib.h
included and I still tried it with casts just in case and that got the same error.
I think the error is when i write(use strcpy
)
This is actually just a side test program because I can't get another program that deals with growing 2d arrays. I get segfaults when I strcpy
int main()
{
char** rec = malloc(10* sizeof(char*));;
char** arr = malloc(10* sizeof(char*));
char a = 'a';
char *c = &a;
for ( int i =0; i < 10; i++){
rec[i] = malloc(10*sizeof(char));
strcpy(rec[i], c);
arr[i] = malloc(10*sizeof(char));
strcpy(arr[i], c);
}
rec = realloc(rec, 20* sizeof(char*));
for (int i =0; i< 20; i++)
rec[i] = realloc(rec, 10*sizeof(char));
arr = realloc(arr, 20* sizeof(char*));
for (int i = 0; i < 20; i++)
arr[i] = realloc(arr, 10*sizeof(char));
for ( int i =0;i< 10; i++ ) {
free(rec[i]);
free(arr[i]);
}
free(rec);free(arr);
}
which gives this bad boy
*** glibc detected *** ./HW1: realloc(): invalid next size: 0x00000000023d0350 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x75be6)[0x7f1150953be6]
/lib/x86_64-linux-gnu/libc.so.6(+0x7b8bc)[0x7f11509598bc]
/lib/x86_64-linux-gnu/libc.so.6(realloc+0xf0)[0x7f1150959bd0]
./HW1[0x40070b]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xfd)[0x7f11508fcead]
./HW1[0x400509]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:2c 8904345153 /import/linux/home/jkim332/cs220/HW1/HW1
00600000-00601000 rw-p 00000000 00:2c 8904345153 /import/linux/home/jkim332/cs220/HW1/HW1
023d0000-023f1000 rw-p 00000000 00:00 0 [heap]
7f114c000000-7f114c021000 rw-p 00000000 00:00 0
7f114c021000-7f1150000000 ---p 00000000 00:00 0
7f11506c8000-7f11506dd000 r-xp 00000000 00:10 4345426042 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f11506dd000-7f11508dd000 ---p 00015000 00:10 4345426042 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f11508dd000-7f11508de000 rw-p 00015000 00:10 4345426042 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f11508de000-7f1150a5f000 r-xp 00000000 00:10 4343317221 /lib/x86_64-linux-gnu/libc-2.13.so
7f1150a5f000-7f1150c5f000 ---p 00181000 00:10 4343317221 /lib/x86_64-linux-gnu/libc-2.13.so
7f1150c5f000-7f1150c63000 r--p 00181000 00:10 4343317221 /lib/x86_64-linux-gnu/libc-2.13.so
7f1150c63000-7f1150c64000 rw-p 00185000 00:10 4343317221 /lib/x86_64-linux-gnu/libc-2.13.so
7f1150c64000-7f1150c69000 rw-p 00000000 00:00 0
7f1150c69000-7f1150c89000 r-xp 00000000 00:10 4345380830 /lib/x86_64-linux-gnu/ld-2.13.so
7f1150e55000-7f1150e58000 rw-p 00000000 00:00 0
7f1150e86000-7f1150e88000 rw-p 00000000 00:00 0
7f1150e88000-7f1150e89000 r--p 0001f000 00:10 4345380830 /lib/x86_64-linux-gnu/ld-2.13.so
7f1150e89000-7f1150e8a000 rw-p 00020000 00:10 4345380830 /lib/x86_64-linux-gnu/ld-2.13.so
7f1150e8a000-7f1150e8b000 rw-p 00000000 00:00 0
7fff359d6000-7fff359f7000 rw-p 00000000 00:00 0 [stack]
7fff359f8000-7fff359fa000 r-xp 00000000 00:00 0 [vdso]
7fff359fa000-7fff359fc000 r--p 00000000 00:00 0 [vvar]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Upvotes: 0
Views: 557
Reputation: 16540
strcpy requires the source string (the one on the right) to be a pointer to a NUL terminated array of characters.
However 'a' is a single character, not a NUL terminated array of character.
For just moving that single character, use 'arr[i] = *c;' OR, for clarity, 'arr[i] = a;'
however, that would be corrupting the pointers to the allocated memory.
If your trying to fill the allocated memory with 'a' then use
memset( arr[i], 'a', 10);
If your only trying to set the first characterin the *arr[i] array, then use
*arr[i] = a; or *arr[i] = 'a';
(note, naming variables with non-meaningful names, such as the posted code, can make the code MUCH harder to read.)
this code:
rec = realloc(rec, 20* sizeof(char*));
for (int i =0; i< 20; i++)
rec[i] = realloc(rec, 10*sizeof(char));
will work for the original 10 rows, but the new rows will contain garbage pointers.
so the new rows will require a malloc to set those pointers 10...19 to point to allocated memory.
as it is, the first 10 rows will realloc just fine, but the last 10 rows are not (yet) valid pointers, so calling realloc on those last 10 rows is undefined behaviour, that, at the least will corrupt the heap and probably result in a seg fault event.
Upvotes: 2