Segmentation Fault

U

user

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!
 
C

Christopher Benson-Manica

(e-mail address removed) 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.
 
P

pete

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.
 
D

Darrell Grainger

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.
 
M

Martin Ambuhl

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.
 
J

Jan Engelhardt

I just finish writing LAB2 with no errors when compiling, but once i run
Stop dereferencing invalid pointers.

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

Peter Pichler

Jan Engelhardt said:
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.
 
J

Jeremy Yallop

Peter said:
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.
 
P

Peter Pichler

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

Your example involves dereferencing an invalid pointer.
 
J

Jeremy Yallop

Peter said:
Your example involves dereferencing an invalid pointer.

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

Jeremy.
 
P

Peter Pichler

Jeremy Yallop said:
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
 
E

Eric Sosman

Peter said:
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.
 
N

nrk

Peter said:
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.
 
M

MSG

Martin Ambuhl said:
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)
 
E

E. Robert Tisdale

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.
 
C

CBFalconer

Jeremy said:
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)"
 
C

CBFalconer

Eric said:
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.
 
E

Emmanuel Delahaye

E. Robert Tisdale said:
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__);
 
P

Peter Nilsson

Emmanuel Delahaye said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,351
Latest member
LolaD32479

Latest Threads

Top