472,799 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,799 software developers and data experts.

A basic C question about segmentation fault

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
6 4948
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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
13
by: Aarti | last post by:
I have a very elementary question about pointers. Please pardon me for my ignorance of C int main() { int* i; *i = 1 //at times this may give me a core dump. const char* str = "test"; //This...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.