LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-05-2010, 11:16 AM   #1
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 37

Rep: Reputation: 0
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
 
Old 08-05-2010, 11:25 AM   #2
dugan
Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 5,005

Rep: Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560
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.

Last edited by dugan; 08-05-2010 at 11:27 AM.
 
Old 08-05-2010, 11:41 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by rahulvishwakarma View Post
/*

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.
 
Old 08-05-2010, 11:47 AM   #4
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 15,105

Rep: Reputation: 2720Reputation: 2720Reputation: 2720Reputation: 2720Reputation: 2720Reputation: 2720Reputation: 2720Reputation: 2720Reputation: 2720Reputation: 2720Reputation: 2720
Quote:
Originally Posted by Sergei Steshenko View Post
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.
 
Old 08-05-2010, 11:58 AM   #5
dugan
Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 5,005

Rep: Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560
Here's a straight answer. Replace this line:

Code:
cp = "abcdefgh";
with this:

Code:
snprintf(cp, 11, "abcdefgh");
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem: *** glibc detected *** free(): invalid pointer: shashank929 Programming 2 04-16-2009 03:09 AM
Error during cimserver start : *** glibc detected *** realloc(): invalid pointer MaverickLikesMustang Linux - Software 0 02-14-2008 07:42 AM
glibc detected : Invalid Pointer Error DragonM15 Linux - Software 3 06-01-2007 01:43 AM
*** glibc detected *** free(): invalid pointer: 0x0804b094 *** lmvent Programming 4 02-03-2007 10:51 PM
OpenOffice.org *** glibc detected *** free(): invalid pointer: 0x41481c94 *** Artanicus Linux - Software 2 02-19-2005 08:04 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration