Easy way to end a c++ program

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"
stop a c++ function
return usually does that.
inside a function that isn't main. using return will just "return" me to main
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
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
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
Thanks for your help guys, I really appreciate it :D
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.
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.