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