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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
08-05-2010, 11:16 AM
|
#1
|
Member
Registered: Aug 2010
Posts: 139
Rep:
|
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
|
|
|
08-05-2010, 11:25 AM
|
#2
|
LQ Guru
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,352
|
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.
|
|
|
08-05-2010, 11:41 AM
|
#3
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by rahulvishwakarma
/*
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.
|
|
|
08-05-2010, 11:47 AM
|
#4
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,405
|
Quote:
Originally Posted by Sergei Steshenko
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.
|
|
|
08-05-2010, 11:58 AM
|
#5
|
LQ Guru
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,352
|
Here's a straight answer. Replace this line:
with this:
Code:
snprintf(cp, 11, "abcdefgh");
|
|
|
All times are GMT -5. The time now is 02:25 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|