error : glibc detected invalid pointer
/*
hey dude error in gnu c pointer please solve this I am using Red Hat Enterprise Linux 5.0. */ #include <stdio.h> #include <stdlib.h> int main() { char * cp = NULL; cp = (char*)malloc(11); cp = "abcdefgh"; printf(" cp = %s \n",cp); free(cp); return 0; } _________________________________________________________________________ gives an error of stack overflow problem :- _______________________________ *** glibc detected *** ./pointer: free(): invalid pointer: 0x08048510 *** ======= Backtrace: ========= /lib/libc.so.6[0x9a0b16] /lib/libc.so.6(cfree+0x90)[0x9a4030] ./pointer[0x8048430] /lib/libc.so.6(__libc_start_main+0xdc)[0x94ddec] ./pointer[0x8048331] ======= Memory map: ======== 00110000-00111000 r-xp 00110000 00:00 0 [vdso] 00915000-0092f000 r-xp 00000000 03:05 2524560 /lib/ld-2.5.so 0092f000-00930000 r-xp 00019000 03:05 2524560 /lib/ld-2.5.so 00930000-00931000 rwxp 0001a000 03:05 2524560 /lib/ld-2.5.so 00938000-00a75000 r-xp 00000000 03:05 2524561 /lib/libc-2.5.so 00a75000-00a77000 r-xp 0013d000 03:05 2524561 /lib/libc-2.5.so 00a77000-00a78000 rwxp 0013f000 03:05 2524561 /lib/libc-2.5.so 00a78000-00a7b000 rwxp 00a78000 00:00 0 00d2b000-00d36000 r-xp 00000000 03:05 2524570 /lib/libgcc_s-4.1.2-20080102.so.1 00d36000-00d37000 rwxp 0000a000 03:05 2524570 /lib/libgcc_s-4.1.2-20080102.so.1 08048000-08049000 r-xp 00000000 03:02 32980 /softwares/data/C/pointer 08049000-0804a000 rw-p 00000000 03:02 32980 /softwares/data/C/pointer 09755000-09776000 rw-p 09755000 00:00 0 b7e00000-b7e21000 rw-p b7e00000 00:00 0 b7e21000-b7f00000 ---p b7e21000 00:00 0 b7fde000-b7fe0000 rw-p b7fde000 00:00 0 b7ff5000-b7ff6000 rw-p b7ff5000 00:00 0 bfdd7000-bfdec000 rw-p bfdd7000 00:00 0 [stack] Aborted |
Code:
/* allocates a block of 10 chars (plus a null) */ |
Quote:
|
Quote:
|
Here's a straight answer. Replace this line:
Code:
cp = "abcdefgh"; Code:
snprintf(cp, 11, "abcdefgh"); |
All times are GMT -5. The time now is 01:07 PM. |