Is it well-defined behaviour to exit the program before main?

后端 未结 1 493
逝去的感伤
逝去的感伤 2021-02-14 01:47

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

相关标签:
1条回答
  • 2021-02-14 02:06

    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.

    0 讨论(0)
提交回复
热议问题