0

return 0 vs exit 0

Hi I had a question about return 0 vs exit 0. My answer was as below: 1. return 0 means entire completion and it also allows to execute "at exit" related functions where as exit 0 is abruption and will not allow such function execution. 2. return 0 means entire completion and it allows to execute destructor calls of stack objects where as exit 0 is abruption and will not allow such function execution. Is my both statement 1 and 2 correct? What is the need to have exit as it abrupt and does not allow code completion specially destructors as well.

14th Sep 2024, 3:43 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 Respostas
+ 1
exit 0 also allows the execution of atexit functions. The major difference is that return 0 will clear all the local objects in stack while exit 0 will only clear those registered at atexit functions
14th Sep 2024, 4:48 PM
RuntimeTerror
RuntimeTerror - avatar
+ 1
Personally, I think the use of exit is to "deliberately" cause a crash or something. I've not seen it been used in real program so I wouldn't know, but that'd be my best guess. edit: Ketan Lalcheta now I remember everything in Godot uses exit whenever an error occurs. now there defence says that "it is indeed deliberate cos they expect everything to run as expected, Incase of undefined, logic error, or any other error, they're okay to crash the program"
14th Sep 2024, 5:28 PM
RuntimeTerror
RuntimeTerror - avatar
0
Thanks. Confused about the need of of the exit. What's the harm of executing destructors ? Why main is not sufficient? Do we need to stop destructors of few stack objects as well by calling exit? What's the harm to call the destructors?
14th Sep 2024, 5:05 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Thanks RuntimeTerror I also never used exit so curious about the same.
14th Sep 2024, 5:30 PM
Ketan Lalcheta
Ketan Lalcheta - avatar