LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   error : glibc detected invalid pointer (https://www.linuxquestions.org/questions/programming-9/error-glibc-detected-invalid-pointer-824387/)

rahulvishwakarma 08-05-2010 10:16 AM

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

dugan 08-05-2010 10:25 AM

Code:

  /* allocates a block of 10 chars (plus a null) */
  /* points cp to it */
  cp = (char*)malloc(11);
 
  /* points cp to a stack-allocated read-only string */
  /* it is now impossible to access the malloc'd block */
  /* the malloc'd block is now a memory leak */
  cp = "abcdefgh";
 
  /* deallocates the read-only, stack allocated string */
  /* this is obviously an illegal operation */
  free(cp);

To copy strings in c, you use strncpy or snprintf. Not the equals operator.

Sergei Steshenko 08-05-2010 10:41 AM

Quote:

Originally Posted by rahulvishwakarma (Post 4056897)
/*

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

You are trying to deallocate memory you haven't allocated.

TB0ne 08-05-2010 10:47 AM

Quote:

Originally Posted by Sergei Steshenko (Post 4056931)
You are trying to deallocate memory you haven't allocated.

And this was answered in the other thread, asking the SAME QUESTION. Don't double-post questions....or ask us to do homework for you.

dugan 08-05-2010 10:58 AM

Here's a straight answer. Replace this line:

Code:

cp = "abcdefgh";
with this:

Code:

snprintf(cp, 11, "abcdefgh");


All times are GMT -5. The time now is 01:07 PM.