Easy way to end a c++ program

Jun 27, 2015 at 6:20pm
Is there an easy way to completely stop a c++ function inside a function that isn't main? It will save me a few "int's"
Jun 27, 2015 at 6:29pm
stop a c++ function
return usually does that.
Jun 27, 2015 at 6:30pm
inside a function that isn't main. using return will just "return" me to main
Jun 27, 2015 at 6:36pm
No it doesn't. Return ends the function that it is in. If that means that the function which called the function that returned reaches its end then it will end too, but return just ends the function that it is in.

To clarify, it is called return because it returns a value from the function, not because it returns back to main. Or it could be because it returns to the function that called it (even if that isn't main), but I'm pretty sure that it is the returning a value thing.
Last edited on Jun 27, 2015 at 6:39pm
Jun 27, 2015 at 6:42pm
exit(int) will end the program and call destructors for static objects. abort() will end the program immediately without calling destructors and will notify the OS that the program has terminated abnormally, which will usually display an error message to the user.
Last edited on Jun 27, 2015 at 6:45pm
Jun 27, 2015 at 6:48pm
call all destructors for objects with automatic storage
It is actually not.
http://en.cppreference.com/w/cpp/utility/program/exit
Stack is not unwound: destructors of variables with automatic storage durations are not called.
That led to some problems with RAII object before when somebody added an exit call deep inside program.

That actually one few things I dislike in C++: exit behavior and no way to proper stop program with stack unwinding at any moment by standard methods.

EDIT: just noticed your edit.
Last edited on Jun 27, 2015 at 6:51pm
Jun 27, 2015 at 7:01pm
Thanks for your help guys, I really appreciate it :D
Jun 27, 2015 at 7:55pm
That actually one few things I dislike in C++: exit behavior and no way to proper stop program with stack unwinding at any moment by standard methods.
I think throwing a control flow exception to be caught in main() is a perfectly acceptable way of unwinding the stack prior to termination.
Jun 27, 2015 at 8:02pm
I think throwing a control flow exception to be caught in main() is a perfectly acceptable way of unwinding the stack prior to termination.
Yeah, it is a most common way. Unless there is catch(...) hidden somewhere.
Topic archived. No new replies allowed.