segmentation fault

Programming, for all ages and all languages.
Post Reply
User avatar
bloodhound23
Member
Member
Posts: 115
Joined: Wed Jan 23, 2008 7:13 pm
Contact:

segmentation fault

Post by bloodhound23 »

I'm using a 64 bit system with Ubuntu. For every program I write that does anything more than

printf("name of program + copyright\n");

when I run it the konsole always says : "core dump segmentation fault"

or something like that , what could be the problem
I thought I wasn't thinking, I thought wrong.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

I suppose the real question is.. what "more" are you doing after the printf? :roll:

Seriously man, http://catb.org/~esr/faqs/smart-questions.html :wink:
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Pyrofan1
Member
Member
Posts: 234
Joined: Sun Apr 29, 2007 1:13 am

Post by Pyrofan1 »

i remember the good old days when i programmed on windows and it accepted code like this

Code: Select all

char *c;

gets(c);
then i switched to linux and it segfault'd
blound
Member
Member
Posts: 70
Joined: Sat Dec 01, 2007 1:36 pm

Post by blound »

c needs to be pointing somewhere valid....
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Coping with Segmentation faluts

Post by DeletedAccount »

Coping up with segmentation faults
-----------------------------------------
You should learn how to use gdb properly ...
when compiling use the g flag .. eg gcc -g <urfile> -o<outputfile>
The run ur program in gdb ... eg gdb outputfile ...
at the gdb prompt use break <line no or func name > to use breakpoints
... use next and step to go through your code ... up and down etc for going up and down frames ... use print <variable name > to view variables in gdb prompt ..use list view source file etc .... There is more to gdb than this ... i just gave a quick summary ... use gdb effectively to sqat out your bugs :P :P
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Post by DeletedAccount »

Pyrofan1 Posted: Mon Feb 11, 2008 4:53 am Post subject:

i remember the good old days when i programmed on windows and it accepted code like this Code:

char *c;

gets(c);


then i switched to linux and it segfault'd
It should segfault for your program's and your good .... When you declare a pointer like that it is pointing to a random address ... It will seg fault in windows also ... but you may be able to run it in dos using turbo C or some legacy compilers ... you should always use malloc before using gets(c) .... You have made a conceptually erraneous statement .... :(
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

Pyrofan1 wrote:i remember the good old days when i programmed on windows and it accepted code like this [...] then i switched to linux and it segfault'd
Means you still don't run your compiler with proper warnings enabled, or you would have caught that problem long before it segfaults. ;-)
Every good solution is obvious once you've found it.
Pyrofan1
Member
Member
Posts: 234
Joined: Sun Apr 29, 2007 1:13 am

Post by Pyrofan1 »

*to all those who replied to my post telling me that i need the pointer to point somewhere, re-read the post and notice how i was talking in the past.
Post Reply