Segmentation fault: where is the bug?

Decked Canoes, Open Canoes, as long as they're canoes!

Moderators: kenneth, sbroam, TheKrikkitWars, Mike W., Sir Adam, KNeal, PAC, adamin

Post Reply
giuliano

Segmentation fault: where is the bug?

Post 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
Judd

Post 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. :)
Jan_dettmer
CBoats Addict
Posts: 350
Joined: Mon Oct 06, 2003 5:47 pm
Location: Vancouver Island
Contact:

Post by Jan_dettmer »

$> cat your_message > /dev/null
Kai

Post 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;
}
Martyn
Pain Boater
Posts: 79
Joined: Mon Dec 01, 2003 2:23 pm
Location: Huntsville, ON

Post by Martyn »

now that's scary - overloading the forum name :wink:
Post Reply