Page 1 of 1

Segmentation fault: where is the bug?

Posted: Mon Feb 02, 2004 1:22 pm
by giuliano
Hi all: these lines of program cause a segfault on my Linux machine:

/* testptr.c */
/* gcc -o testptr -Wall -g testptr.c */
int main() {
char *name = "Giuliano";
*name = 'X'; //here I get a seg. fault
return 0;
}

I've tried to found the bug using GDB:
gdb testptr
Starting program: testptr
Breakpoint 1, main () at testptr.c:2
2 char *name = "Giuliano";
(gdb) step
3 *name = 'X';
(gdb) step
4 return 0;
(gdb) print name
$1 = 0x80483e8 "Xiuliano"
(gdb) step
5 }
(gdb) step
0x400397f7 in __libc_start_main () from /lib/i686/libc.so.6
(gdb) step
Single stepping until exit from function __libc_start_main,
which has no line number information.

Program exited normally. (No seg. fault !!!!!!!!!!!!!!)

So, where is the problem?
Thank you
Giuliano

Posted: Mon Feb 02, 2004 2:44 pm
by Judd
umm. maybe you didn't look real close to this site before you posted..... notice a lot of information about paddling and boats, and very little information on writing c programs. :)

Posted: Mon Feb 02, 2004 4:44 pm
by Jan_dettmer
$> cat your_message > /dev/null

Posted: Mon Feb 02, 2004 6:14 pm
by Kai
Try this:

- first some open boating on some nice river. enjoy nature. free your mind.

then you might consider using the data heap:

/* testptr.c */
/* gcc -o testptr -Wall -g testptr.c */
int main() {
char name [] = "Giuliano";
name[0] = 'X'; //here I get a seg. fault
return 0;
}

Posted: Mon Feb 02, 2004 8:22 pm
by Martyn
now that's scary - overloading the forum name :wink: