The code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char * cp = malloc(sizeof * cp * 20);
cp="Hello\0";
free(cp);
return 0;
}
The error:
*** glibc detected *** ./mallocTest: free(): invalid pointer: 0x000000000040069c ***
======= Backtrace: =========
/lib/libc.so.6(+0x71496)[0x7f92ee448496]
/lib/libc.so.6(cfree+0x6c)[0x7f92ee44d29c]
./mallocTest[0x40059e]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7f92ee3f5c3d]
./mallocTest[0x4004b9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:08 3162120 /home/gabriel/Programming/C/mallocTest
00600000-00601000 rw-p 00000000 08:08 3162120 /home/gabriel/Programming/C/mallocTest
01327000-01348000 rw-p 00000000 00:00 0 [heap]
7f92e8000000-7f92e8021000 rw-p 00000000 00:00 0
7f92e8021000-7f92ec000000 ---p 00000000 00:00 0
7f92ee3d7000-7f92ee52a000 r-xp 00000000 08:08 2452227 /lib/libc-2.12.2.so
7f92ee52a000-7f92ee729000 ---p 00153000 08:08 2452227 /lib/libc-2.12.2.so
7f92ee729000-7f92ee72d000 r--p 00152000 08:08 2452227 /lib/libc-2.12.2.so
7f92ee72d000-7f92ee72e000 rw-p 00156000 08:08 2452227 /lib/libc-2.12.2.so
7f92ee72e000-7f92ee733000 rw-p 00000000 00:00 0
7f92ee733000-7f92ee748000 r-xp 00000000 08:08 33223 /usr/lib/libgcc_s.so.1
7f92ee748000-7f92ee948000 ---p 00015000 08:08 33223 /usr/lib/libgcc_s.so.1
7f92ee948000-7f92ee949000 rw-p 00015000 08:08 33223 /usr/lib/libgcc_s.so.1
7f92ee949000-7f92ee9ca000 r-xp 00000000 08:08 2449430 /lib/libm-2.12.2.so
7f92ee9ca000-7f92eebc9000 ---p 00081000 08:08 2449430 /lib/libm-2.12.2.so
7f92eebc9000-7f92eebca000 r--p 00080000 08:08 2449430 /lib/libm-2.12.2.so
7f92eebca000-7f92eebcb000 rw-p 00081000 08:08 2449430 /lib/libm-2.12.2.so
7f92eebcb000-7f92eecb7000 r-xp 00000000 08:08 33213 /usr/lib/libstdc++.so.6.0.14
7f92eecb7000-7f92eeeb6000 ---p 000ec000 08:08 33213 /usr/lib/libstdc++.so.6.0.14
7f92eeeb6000-7f92eeebe000 r--p 000eb000 08:08 33213 /usr/lib/libstdc++.so.6.0.14
7f92eeebe000-7f92eeec0000 rw-p 000f3000 08:08 33213 /usr/lib/libstdc++.so.6.0.14
7f92eeec0000-7f92eeed5000 rw-p 00000000 00:00 0
7f92eeed5000-7f92eeef3000 r-xp 00000000 08:08 2449712 /lib/ld-2.12.2.so
7f92ef0be000-7f92ef0c3000 rw-p 00000000 00:00 0
7f92ef0f1000-7f92ef0f2000 rw-p 00000000 00:00 0
7f92ef0f2000-7f92ef0f3000 r--p 0001d000 08:08 2449712 /lib/ld-2.12.2.so
7f92ef0f3000-7f92ef0f4000 rw-p 0001e000 08:08 2449712 /lib/ld-2.12.2.so
7f92ef0f4000-7f92ef0f5000 rw-p 00000000 00:00 0
7fff6b352000-7fff6b373000 rw-p 00000000 00:00 0 [stack]
7fff6b3ff000-7fff6b400000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted
I’m taking that the pointer is getting corrupted somehow before freeing it, but I have no idea how. Doing nothing with it, and freeing it immediately after mallocing it works fine, so I take it I’m putting the string value in wrong.
Sorry to be such a newb. Any help is greatly appreciated.
Oh, and I have no idea why the standard C++ library is being included in that error, but that’s not the problem. I tried compiling it manually is gcc and got the same error, minus the libstdc++. Code::Blocks is being stupid.
Here
cpis declared as achar*and initialized to point to a dynamically allocated array of 20char.Now you assign
cpto point to a fixed string, losing the previous value ofcp. This is where a memory leak is caused.This now attempts to
freethe fixed string, but this is illegal. You can onlyfreememory allocated bymalloc(orcallocorrealloc).