By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,836 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,836 IT Pros & Developers. It's quick & easy.

Segmentation Fault

P: n/a
I just finish writing LAB2 with no errors when compiling, but once i run
it, i get "segmentation fault".. i don't know what is wrong, can anyoen
tell me what it means and how i can fix it? thx!

Nov 14 '05 #1
Share this Question
Share on Google+
21 Replies


P: n/a
us**@domain.invalid spoke thus:
I just finish writing LAB2 with no errors when compiling, but once i run
it, i get "segmentation fault".. i don't know what is wrong, can anyoen
tell me what it means and how i can fix it? thx!


You inadvertently drew a pentagram on your screen and thereby summoned
The Lord of Vague Usenet Postings, causing your error. That's just a
guess, however. Another guess is that LAB2, whatever the heck it was,
involved pointers or memory allocation, and that you messed it up.
"Segmentation fault" means that you did something wrong, but nothing
more can be gleaned from it without looking at your code. If you post
it, someone may be kind enough to help you.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #2

P: n/a
us**@domain.invalid wrote:

I just finish writing LAB2 with no errors when compiling,
but once i run it, i get "segmentation fault"
.. i don't know what is wrong, can anyoen
tell me what it means and how i can fix it? thx!


Write the smallest similar program that compiles
and gives the same runtime error. Then post the code.

--
pete
Nov 14 '05 #3

P: n/a
On Fri, 6 Feb 2004 us**@domain.invalid wrote:
I just finish writing LAB2 with no errors when compiling, but once i run
it, i get "segmentation fault".. i don't know what is wrong, can anyoen
tell me what it means and how i can fix it? thx!


It means you made a mistake in your program. The fix is to stop writing
bad code. Without further information I cannot be more specific.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #4

P: n/a
us**@domain.invalid wrote:
I just finish writing LAB2 with no errors when compiling, but once i run
it, i get "segmentation fault".. i don't know what is wrong, can anyoen
tell me what it means and how i can fix it? thx!


Stop dereferencing invalid pointers.
--
Martin Ambuhl
Nov 14 '05 #5

P: n/a
>> I just finish writing LAB2 with no errors when compiling, but once i run
it, i get "segmentation fault".. i don't know what is wrong, can anyoen
tell me what it means and how i can fix it? thx!


Stop dereferencing invalid pointers.


What a guess :)
Do the statistics say so that it's most likely to be an invalid pointer?
Nov 14 '05 #6

P: n/a
"Jan Engelhardt" <je*****@linux01.gwdg.de> wrote in message
news:Pi******************************@yvahk01.tjqt .qr...
I just finish writing LAB2 with no errors when compiling, but once i run it, i get "segmentation fault".. i don't know what is wrong, can anyoen
tell me what it means and how i can fix it? thx!


Stop dereferencing invalid pointers.


What a guess :)
Do the statistics say so that it's most likely to be an invalid pointer?


Yes. The statistics show that 100% of segmentation faults are caused by
derefencing an invalid pointer.
Nov 14 '05 #7

P: n/a
Peter Pichler wrote:
"Jan Engelhardt" <je*****@linux01.gwdg.de> wrote in message
news:Pi******************************@yvahk01.tjqt .qr...
>> I just finish writing LAB2 with no errors when compiling, but once i run >> it, i get "segmentation fault".. i don't know what is wrong, can anyoen
>> tell me what it means and how i can fix it? thx!
>
>Stop dereferencing invalid pointers.


What a guess :)
Do the statistics say so that it's most likely to be an invalid pointer?


Yes. The statistics show that 100% of segmentation faults are caused by
derefencing an invalid pointer.


$ cat recurse.c
int main() { return main(); }
$ gcc -ansi -pedantic recurse.c
$ ./a.out
Segmentation fault

Jeremy.
Nov 14 '05 #8

P: n/a
"Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> wrote in message
news:sl*******************@hehe.cl.cam.ac.uk...
Peter Pichler wrote:

Yes. The statistics show that 100% of segmentation faults are caused by
derefencing an invalid pointer.


$ cat recurse.c
int main() { return main(); }
$ gcc -ansi -pedantic recurse.c
$ ./a.out
Segmentation fault


Your example involves dereferencing an invalid pointer.
Nov 14 '05 #9

P: n/a
Peter Pichler wrote:
"Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> wrote in message
news:sl*******************@hehe.cl.cam.ac.uk...
Peter Pichler wrote:
>
> Yes. The statistics show that 100% of segmentation faults are caused by
> derefencing an invalid pointer.


$ cat recurse.c
int main() { return main(); }
$ gcc -ansi -pedantic recurse.c
$ ./a.out
Segmentation fault


Your example involves dereferencing an invalid pointer.


Really? At which point, and for what reason, does the pointer become
invalid?

Jeremy.
Nov 14 '05 #10

P: n/a
"Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> wrote in message
news:sl*******************@hehe.cl.cam.ac.uk...
Peter Pichler wrote:
"Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> wrote in message
news:sl*******************@hehe.cl.cam.ac.uk...
Peter Pichler wrote:
>
> Yes. The statistics show that 100% of segmentation faults are caused by > derefencing an invalid pointer.

$ cat recurse.c
int main() { return main(); }
$ gcc -ansi -pedantic recurse.c
$ ./a.out
Segmentation fault


Your example involves dereferencing an invalid pointer.


Really? At which point, and for what reason, does the pointer become
invalid?


Isn't calling a function dereferencing a function pointer?
This is a genuine question, I am not so sure any more.

Peter
Nov 14 '05 #11

P: n/a
nrk
Peter Pichler wrote:
"Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> wrote in message
news:sl*******************@hehe.cl.cam.ac.uk...
Peter Pichler wrote:
> "Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> wrote in message
> news:sl*******************@hehe.cl.cam.ac.uk...
>> Peter Pichler wrote:
>> >
>> > Yes. The statistics show that 100% of segmentation faults are caused by >> > derefencing an invalid pointer.
>>
>> $ cat recurse.c
>> int main() { return main(); }
>> $ gcc -ansi -pedantic recurse.c
>> $ ./a.out
>> Segmentation fault
>
> Your example involves dereferencing an invalid pointer.
Really? At which point, and for what reason, does the pointer become
invalid?


Isn't calling a function dereferencing a function pointer?
This is a genuine question, I am not so sure any more.


I believe you're correct. (Since, I am watching my mouth as far as
interpreting the standard for the general public goes, I won't assert that
you're correct :-)

From 6.3.2.1:

4 A function designator is an expression that has function type. Except when
it is the operand of the sizeof operator54) or the unary & operator, a
function designator with type "function returning type" is converted to an
expression that has type "pointer to function returning type".

Further, 6.5.2.2 Function Calls:

1 The expression that denotes the called function77) shall have type pointer
to function returning void or returning an object type other than an array
type.

77) Most often, this is the result of converting an identifier that is a
function designator.

-nrk.
Peter


--
Remove devnull for email
Nov 14 '05 #12

P: n/a
Peter Pichler wrote:

"Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> wrote in message
news:sl*******************@hehe.cl.cam.ac.uk...
Peter Pichler wrote:
"Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> wrote in message
news:sl*******************@hehe.cl.cam.ac.uk...
> Peter Pichler wrote:
> >
> > Yes. The statistics show that 100% of segmentation faults are caused by> > derefencing an invalid pointer.
>
> $ cat recurse.c
> int main() { return main(); }
> $ gcc -ansi -pedantic recurse.c
> $ ./a.out
> Segmentation fault

Your example involves dereferencing an invalid pointer.


Really? At which point, and for what reason, does the pointer become
invalid?


Isn't calling a function dereferencing a function pointer?
This is a genuine question, I am not so sure any more.


The second appearance of `main' is in fact a function
pointer, and the `()' function-call operator uses that pointer
value to determine what function to invoke. Whether this use
constitutes a "dereference" is unclear -- the Standard neither
defines nor uses the word.

At any rate, the function pointer value `main' remains
valid for the life of the program. Thus, the call is not
"dereferencing an invalid pointer" because the pointer is
not invalid.

--
Er*********@sun.com
Nov 14 '05 #13

P: n/a
On 6 Feb 2004 22:22:58 GMT, in comp.lang.c , Jeremy Yallop
<je****@jdyallop.freeserve.co.uk> wrote:
Peter Pichler wrote:
"Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> wrote in message
news:sl*******************@hehe.cl.cam.ac.uk...
Peter Pichler wrote:
>
> Yes. The statistics show that 100% of segmentation faults are caused by
> derefencing an invalid pointer.

$ cat recurse.c
int main() { return main(); }
$ gcc -ansi -pedantic recurse.c
$ ./a.out
Segmentation fault


Your example involves dereferencing an invalid pointer.


Really? At which point, and for what reason, does the pointer become
invalid?


when you run out of memory .

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #14

P: n/a
MSG
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<DT*******************@newsread1.news.atl.ear thlink.net>...
us**@domain.invalid wrote:
I just finish writing LAB2 with no errors when compiling, but once i run
it, i get "segmentation fault".. i don't know what is wrong, can anyoen
tell me what it means and how i can fix it? thx!


Stop dereferencing invalid pointers.


What happened to our OTNs (off-topic Nazis). Why aren't they stomping
the guts out of these people saying "segmentation ain't in our
bible^Wstandard!" :-)

MSG (no, I'm not an OTN)
Nov 14 '05 #15

P: n/a
us**@domain.invalid wrote:
I just finish writing LAB2 with no errors when compiling
but once i run it, I get "segmentation fault".
I don't know what is wrong,
Can anyone tell me what it means and how I can fix it?


Your program is attempting to reference memory
which doesn't belong to it.
You probably have an array index which is "out-of-bounds".
Use your debugger. If you don't have a debugger or know how to use it,
you can insert statements to print diagnostic messages

fprintf(stderr, "Got to this point in the program!\n"); fflush(stderr);

Put the first one smack dab in the middle of your code.
If you get the "segmentation fault" before the diagnostic message
you know that the error is before the print statement and
the error is after the print statement
if you get the diagnostic message before the "segmentation fault".
A short binary search usually locates the error quickly.

Nov 14 '05 #16

P: n/a
Jeremy Yallop wrote:
Peter Pichler wrote:
"Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> wrote
Peter Pichler wrote:
>
> Yes. The statistics show that 100% of segmentation faults
> are caused by derefencing an invalid pointer.

$ cat recurse.c
int main() { return main(); }
$ gcc -ansi -pedantic recurse.c
$ ./a.out
Segmentation fault


Your example involves dereferencing an invalid pointer.


Really? At which point, and for what reason, does the pointer
become invalid?


Also, what pointer? To make sure, add void in "int main(void)"

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #17

P: n/a
Eric Sosman wrote:
Peter Pichler wrote:
"Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> wrote
Peter Pichler wrote:
> "Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> wrote
>> Peter Pichler wrote:
>> >
>> > Yes. The statistics show that 100% of segmentation faults
>> > are caused by derefencing an invalid pointer.
>>
>> $ cat recurse.c
>> int main() { return main(); }
>> $ gcc -ansi -pedantic recurse.c
>> $ ./a.out
>> Segmentation fault
>
> Your example involves dereferencing an invalid pointer.

Really? At which point, and for what reason, does the
pointer become invalid?


Isn't calling a function dereferencing a function pointer?
This is a genuine question, I am not so sure any more.


The second appearance of `main' is in fact a function
pointer, and the `()' function-call operator uses that pointer
value to determine what function to invoke. Whether this use
constitutes a "dereference" is unclear -- the Standard neither
defines nor uses the word.

At any rate, the function pointer value `main' remains
valid for the life of the program. Thus, the call is not
"dereferencing an invalid pointer" because the pointer is
not invalid.


I was going to be a smart-ass and point out that compiling with
gcc -O2 did not generate the segment fault, because of tail
recursion optimization. Luckily I tried it first, and found that
gcc does NOT optimize this one. Must be a feature, couldn't
possibly be a bug.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #18

P: n/a
In 'comp.lang.c', "E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:
fprintf(stderr, "Got to this point in the program!\n"); fflush(stderr);


I'm not sure thar stderr is buffered, hence the fflush() seems redunant.
Also, the line is terminated by a '\n', that makes the fflush() twice
redundant. What would be more informative would have been to add smart
information like the file name and the line in the file:

fprintf (stderr, "Got to here %s:%d in the program!\n", __FILE__, __LINE__);

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #19

P: n/a
"Emmanuel Delahaye" <em**********@noos.fr> wrote in message
news:Xn***************************@212.27.42.68...
In 'comp.lang.c', "E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:
fprintf(stderr, "Got to this point in the program!\n"); fflush(stderr);


I'm not sure thar stderr is buffered, hence the fflush() seems redunant.
Also, the line is terminated by a '\n', that makes the fflush() twice
redundant. What would be more informative would have been to add smart
information like the file name and the line in the file:

fprintf (stderr, "Got to here %s:%d in the program!\n", __FILE__,

__LINE__);

A nitpick here is that __LINE__ is not guaranteed to have int type.

--
Peter
Nov 14 '05 #20

P: n/a
On 6 Feb 2004 17:52:11 -0800, ms*****@yahoo.com (MSG) wrote:
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<DT*******************@newsread1.news.atl.ear thlink.net>...
us**@domain.invalid wrote:
> I just finish writing LAB2 with no errors when compiling, but once i run
> it, i get "segmentation fault".. i don't know what is wrong, can anyoen
> tell me what it means and how i can fix it? thx!
>


Stop dereferencing invalid pointers.


What happened to our OTNs (off-topic Nazis). Why aren't they stomping
the guts out of these people saying "segmentation ain't in our
bible^Wstandard!" :-)

You must distinguish between the thread title ("Segmentation Fault")
and the topic (invalid pointers.)

BTW, what's the "^W" for?

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #21

P: n/a
Alan Balmer <al******@att.net> scribbled the following:
On 6 Feb 2004 17:52:11 -0800, ms*****@yahoo.com (MSG) wrote:
What happened to our OTNs (off-topic Nazis). Why aren't they stomping
the guts out of these people saying "segmentation ain't in our
bible^Wstandard!" :-)
You must distinguish between the thread title ("Segmentation Fault")
and the topic (invalid pointers.)

BTW, what's the "^W" for?


Old "ed" command, meaning "delete the previous word". Understood in
informal context to mean "no, I mean".

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I am lying."
- Anon
Nov 14 '05 #22

This discussion thread is closed

Replies have been disabled for this discussion.