3

What does the C int main() function return when the program hits a run-time exception (e.g. segmentation fault)?

When searching for an answer I hit many discussions/posts about what main() should return, like this one.

Joel
  • 5,732
  • 4
  • 37
  • 65
meaning-matters
  • 21,929
  • 10
  • 82
  • 142
  • 1
    Related (if not duplicate): [What error code does a process that segfaults return?](https://stackoverflow.com/questions/14599670/what-error-code-does-a-process-that-segfaults-return). – Martin R Apr 16 '19 at 08:15
  • What platform are you asking about ? Linux ? Can you add the relevant tag(s) ? – Sander De Dycker Apr 16 '19 at 08:19
  • @SanderDeDycker Not targeting a particular platform. – meaning-matters Apr 16 '19 at 08:21
  • @meaning-matters : then this question is not answerable : hardware exceptions are not covered by the C standard. – Sander De Dycker Apr 16 '19 at 08:32
  • @SanderDeDycker The C standard doesn't say anything about accessing null pointers, divide-by-zero, ...? – meaning-matters Apr 16 '19 at 08:37
  • @meaning-matters : the C standard refers to [dereferencing a null pointer](https://stackoverflow.com/questions/6793262/why-dereferencing-a-null-pointer-is-undefined-behaviour) and [division by zero](https://stackoverflow.com/questions/3004095/division-by-zero-undefined-behavior-or-implementation-defined-in-c-and-or-c) (as well as other such hardware exceptions) as [undefined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) - ie. not covered by the C standard. – Sander De Dycker Apr 16 '19 at 08:52

2 Answers2

9

It (the main function) doesn't return if the program crashes. If a program crashes, then the operating system would have killed the program, so the program isn't running anymore and that includes the main function. A program that doesn't run can't return anything on its own. What is "returned" to the running environment depends on the operating system, which have taken over after the program.

Whatever is returned is handled by the operating system. For POSIX systems, a process that is killed by a signal (like SIGSEGV, segmentation fault) the OS will return 128 plus the signal number. This is documented in e.g. this waitpid reference page (and the links from it).

For Windows it's typically reported as a cryptic long value (usually the value 0x80000000 plus other flags and data).

For older or more primitive operating systems that don't handle crashes, the value being "returned" is usually what happen to be in the "return value" register or on top of the stack at the time of the crash.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Sure, but what value is passed to the system (which can be caught in a shell on certain platforms)? – meaning-matters Apr 16 '19 at 08:12
  • 1
    The status from the wait functions indicates that the process died from a signal and which signal it was. Look at the man page for `wait()` or `waitpid()` or any alternative. – Jonathan Leffler Apr 16 '19 at 08:15
  • @meaning-matters When it comes to shells, if a process terminates because of a signal, posix says that `$?` will be a number greater than 128. bash uses 128+the signal number; others might too. – Shawn Apr 16 '19 at 08:40
  • @Someprogrammerdude You should move your last comment to the answer. That is what OP is actually wondering. – klutt Apr 16 '19 at 08:50
  • I feel like this answer is more suited to answer a question like "what happens when a program crashes". The original question **what does main return on run time exception** still remains (in my opinion) foreshadowed of the extended edit of this answer which is targeting what is happening within a given OS (which i perceive to be a different question). Also the first statement is incorrect. The main DOES return something. – Joel Apr 16 '19 at 10:05
  • 1
    @Joel If a program crashes, it doesn't run anymore. That includes the `main` function, it simply *can't* return anything if it's not running. – Some programmer dude Apr 16 '19 at 10:07
0

If a program crashes, it will not return any value. It will be up to the operating system to handle that, but the C standard does not say anything about what should happen. When calling a program, the behavior can be described like this:

int mainWrapper() {
    int ret;
    try {
        ret = main();
    }
    catch(Exception e) {
        ret = // Some value that may or may not depend on what 
              // happened and might even be random
    }
    return ret;
}

What happens in reality when you call a function in machine code is that you simply store the address of where in the code you are at a certain place and then do a jump to the code you want to execute. That code may have some assumptions about some registers containing the arguments you want to pass to that function. If you forgot to load those registers, the code will use whatever was there without knowing that you forgot to send the arguments. Same thing goes when the function ends. This happens when the machine code hits a certain return instruction. This instruction will jump back to where we called it. If the code after that expects the function to return something, it will basically just hope that the function stored its return value at the right place and then read it without knowing if this is the case.

So the short answer is that it might contain a random value or something that is decided by the operating system.

klutt
  • 30,332
  • 17
  • 55
  • 95