Is it well-defined behaviour to exit the program b

2019-03-25 12:06发布

问题:

It's definitely possible to execute code before main is called, as seen by many examples in this question.

However, what if in that pre-main code, the program is told to exit via std::exit or std::abort? Since main is defined as the start of a program, what consequences are there from exiting before the start?

Upon printing something in each section, I get the following results:

Format:
Section: output

Main: main
Init (called before main): init
Exit (set up with std::atexit inside Init): exiting



Sample runs:

Init called without exiting:

init
main
returns 0

Init calls std::exit(0):

init
returns 0

Init calls std::abort:

init
crashes and returns 3 on Windows with GCC 4.7.2
crashes and brings up the usual box with VS11
returns 0 on LiveWorkSpace

Init sets handler and calls std::exit(0):

init
exiting
returns 0

Init sets handler and calls std::abort:

init
same deal as without the handler

While searching, I saw this question: Is there any way a C/C++ program can crash before main()?. However, it doesn't answer what I want to know: Is any of this behaviour, calling std::exit or std::abort before main, well-defined? Is any of this undefined behaviour?

回答1:

The short answer is: there are (almost) no consequences. Some destructors may not be called if you unexpectedly call exit, but that's pretty much it.
Generally, not calling destructors is not the cleanest possible way, but then again the end result will be the same.

When a process terminates (via exit or abort or simply by segfaulting, or another reason), handles (kernel objects, files, etc.) are closed, and the memory associated with the program's address space is reclaimed by the operating system.

There is not much else to it either, because when you call exit or abort, you're basically requesting that the program terminates (these functions never return!) so you really can't expect anything to happen thereafter.

Note that registering a function like Init to be called before main is non-standard stuff, but you can get the same effect by having a constructor in a global.