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

A basic C question about segmentation fault

P: n/a
int main()
{
printf("\n Hello World");
main;
return 0;
}

This program terminate just after one loop while the second program
goes on infinitely untill segmentation fault (core dumped) on gcc. The
only difference is that in first I only call "main" and in second call
"main()".

int main()
{
printf("\n Hello World");
main();
return 0;
}

I partially understand that in the second program the stack becomes
full and it return the program.

In what all cases does segmentation fault occurs. I had seen it at so
many different places for so many mistakes of mine that I want to
understand what exactly happens when a segmentation fault occurs. Is
it that there is no more memory for this program or the stack
overflows or the limit of MAX recursion has reached an end ?

Mar 1 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
DanielJohnson wrote:
int main()
{
printf("\n Hello World");
main;
return 0;
}

This program terminate just after one loop while the second program
goes on infinitely untill segmentation fault (core dumped) on gcc. The
only difference is that in first I only call "main"
No, you don't: there's no call there. `main;` is a statement that
evaluates the expression `main` (whose rvalue is a pointer to the
function `main`) and then discards it.

Just as `printf( "\n Hello World" );` is a statement that evaluates
the expression `printf( "\n Hello World" )` [which, note, may
produce no visible output, because you never output a final `\n`,
nor do you flush `stdout`], and discards its result.
and in second call "main()".
Indeed you do.
int main()
{
printf("\n Hello World");
main();
return 0;
}

I partially understand that in the second program the stack becomes
full and it return the program.
In a typical implementation, that's what usually happens -- the
program dies for lack of resources. Note that a suitably clever
compiler /could/ optimise this code into

while(1) printf( "\n Hello World" );

for which the resource that will be exhausted will be your patience.
In what all cases does segmentation fault occurs. I had seen it at so
many different places for so many mistakes of mine that I want to
understand what exactly happens when a segmentation fault occurs. Is
it that there is no more memory for this program or the stack
overflows or the limit of MAX recursion has reached an end ?
It's implementation specific -- and so, outside the Standard -- but
"improper memory handling": eg dereferencing an invalid pointer
(which will happen when you run out of stack ...).

Learn not to reference memory that doesn't belong to you.

--
Chris "electric hedgehog" Dollin
"If there is a problem, you must confess it, Mr Chaplin."
Mr Carter, /The Beiderbeck Affair/

Mar 1 '07 #2

P: n/a
"DanielJohnson" <di********@gmail.comwrote:

[ That's a misleading subject, BTW. It's not really about a segfault. ]
int main()
{
printf("\n Hello World");
main;
return 0;
}

This program terminate just after one loop while the second program
goes on infinitely untill segmentation fault (core dumped) on gcc. The
only difference is that in first I only call "main" and in second call
"main()".
Nope. The difference is that in the first you don't call main at all.
You evaluate the expression main. Any function name, including that of
main, decays to a function pointer in nearly all circumstances. In this
case, the only thing you do with that function pointer is to find its
value in the context of an expression statement - and since you do
nothing with it, it's forgotten immediately. It's no different, in this
case, from if you had written any_existing_variable_name;, or 42;.

In your other program, you had main(); instead of main;. That's also an
expression statement: it consists of the function pointer main, to which
the function call operator () is applied. So in that case, main() _does_
get called. Infinitely recursively, in your case.
In what all cases does segmentation fault occurs.
That's rather system-dependent and not specified by ISO C (in fact, ISO
C doesn't specify anything called a "segmentation fault" at all); but in
general, it occurs when you do something wrong with memory. This is a
rather large area, so a list of "all cases", even if not system-
dependent, would be hard to post accurately. Overflowing the call stack,
as you did in your second program, can - but is not required to! - do
it.

Richard
Mar 1 '07 #3

P: n/a
DanielJohnson wrote On 03/01/07 09:28,:
int main()
{
printf("\n Hello World");
main;
return 0;
}

This program terminate just after one loop while the second program
goes on infinitely untill segmentation fault (core dumped) on gcc. The
only difference is that in first I only call "main" and in second call
"main()".

int main()
{
printf("\n Hello World");
main();
return 0;
}

I partially understand that in the second program the stack becomes
full and it return the program.

In what all cases does segmentation fault occurs. I had seen it at so
many different places for so many mistakes of mine that I want to
understand what exactly happens when a segmentation fault occurs. Is
it that there is no more memory for this program or the stack
overflows or the limit of MAX recursion has reached an end ?
"Segmentation fault" (and "bus error" and "general
protection fault" and so on) are not part of C; the C
language does not specify them or describe them or say
anything about what provokes them. They are the host
system's responses to program misbehavior of various kinds.
C does not say what happens when the code misbehaves; it
just says "undefined behavior" and leaves everything to
the discretion of the host environment.

<off-topic>

On some systems, a "segment violation" means that the
program tried to access a non-existent memory location,
an address that the program doesn't own. There are many
possible program errors that could cause this: trying to
follow a NULL pointer or an uninitialized pointer, trying
to use an absurd array index, attempting infinite recursion,
and so on.

Why is a simple idea like "invalid address" dressed up
in the opaque phrase "segment violation" or the even more
cryptic "SIGSEGV?" Because it's geekspeak. Just accept it,
and have faith that someday when you learn more about how
virtual memory systems are implemented it may make sense.

</off-topic>

--
Er*********@sun.com
Mar 1 '07 #4

P: n/a
Actaully the term segmentation fault does make sense. On many
computer systems memory is broken up into segments, see a discussion
on virtual memory implementations. If you try to access a segment
that doesn't belong to you then a segmentation fault occurs. Read up
on virtual memory and how it is implemented for more information, any
decent computer architecture book should have a section somewhere on
segmented or paged segmented virtual memory. Correctly stated above,
this problem has nothing special to do with C.

Mar 2 '07 #5

P: n/a
On Mar 1, 7:28 pm, "DanielJohnson" <diffuse...@gmail.comwrote:
int main()
{
printf("\n Hello World");
main;
return 0;

}

This program terminate just after one loop while the second program
goes on infinitely untill segmentation fault (core dumped) on gcc. The
only difference is that in first I only call "main" and in second call
"main()".

int main()
{
printf("\n Hello World");
main();
return 0;

}

I partially understand that in the second program the stack becomes
full and it return the program.

In what all cases does segmentation fault occurs. I had seen it at so
in any case when the program tries to access memory not own by you
(oversimplified),
this is very much simplest link you may want to refer

http://en.wikipedia.org/wiki/Segmentation_fault
http://en.wikipedia.org/wiki/Buffer_overflow
http://en.wikipedia.org/wiki/Memory_leak
many different places for so many mistakes of mine that I want to
understand what exactly happens when a segmentation fault occurs. Is
Normaylly your OS will not allow to do anything nasty, so it will
normally Exit the program.

Better question : How to avoid Segmentation fault ?
it that there is no more memory for this program or the stack
overflows or the limit of MAX recursion has reached an end ?
Yes, Coneptually there are correct ,but it is hard to give exact
answer.

Mar 2 '07 #6

P: n/a
Sheth Raxit wrote:
On Mar 1, 7:28 pm, "DanielJohnson" <diffuse...@gmail.comwrote:
int main()
{
printf("\n Hello World");
main;
return 0;

}

This program terminate just after one loop while the second program
goes on infinitely untill segmentation fault (core dumped) on gcc. The
only difference is that in first I only call "main" and in second call
"main()".

int main()
{
printf("\n Hello World");
main();
return 0;

}

I partially understand that in the second program the stack becomes
full and it return the program.

In what all cases does segmentation fault occurs. I had seen it at so

in any case when the program tries to access memory not own by you
(oversimplified),
Or try to write to read-only portions of the process's address space.
this is very much simplest link you may want to refer

http://en.wikipedia.org/wiki/Segmentation_fault
http://en.wikipedia.org/wiki/Buffer_overflow
http://en.wikipedia.org/wiki/Memory_leak
IMHO, the last link is unrelated to the first two.

<snip>
it that there is no more memory for this program or the stack
overflows or the limit of MAX recursion has reached an end ?

Yes, Coneptually there are correct ,but it is hard to give exact
answer.
What is conceptually correct?

Mar 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.