You are not logged in.
Pages: 1
Hello. I've googled around and have found that exit will exit the program no matter what. Now this seems like an excuse to have bad code, but I'm unsure. What should I use? exit or return to main and then return from that.
Personally, I'd rather be back in Hobbiton.
Offline
I've seen it done both ways, there is a difference between the two functions, but it's subtle and usually doesn't matter.
When you call return in main(), destructors will be called for locally scoped objects. If you call exit(), no destructor will be called for locally scoped objects. exit() does not return. That means that once I call it, there are "no backsies." Any objects that you've created in that function will not be destroyed. Often this has no implications, but sometimes it does, like closing files
Hofstadter's Law:
It always takes longer than you expect, even when you take into account Hofstadter's Law.
Offline
exit() will run in reverse order the functions that are set with atexit(), before exiting.
_exit() will exit "immediately".
I think return from main and exit() in main are equivalent. I see nothing wrong with using exit() inside functions as long as you know exactly where it exited (when debugging, etc). It would in a sense be equivalent to an assert.
Offline
exit() will run in reverse order the functions that are set with atexit(), before exiting.
_exit() will exit "immediately".
... and is an implementation extension. Use abort().
I think return from main and exit() in main are equivalent. I see nothing wrong with using exit() inside functions as long as you know exactly where it exited (when debugging, etc). It would in a sense be equivalent to an assert.
I would use assert/abort for debugging, return in main, and avoid using exit at all. It comes in handy every once in a while, but I would consider heavy use of atexit() and exit() to be indicative of poor code structure.
Hmm... this makes me wonder... could one use exit/atexit combined with setjmp/longjmp to make a poor man's exception handler? Hideous! ![]()
Offline
abort/assert? NOT MORE stdlib.h FUNCTIONS! Lol, jk. Thanks for the info, I think I understand now.
Personally, I'd rather be back in Hobbiton.
Offline
Themaister wrote:exit() will run in reverse order the functions that are set with atexit(), before exiting.
_exit() will exit "immediately".
... and is an implementation extension. Use abort().
I think return from main and exit() in main are equivalent. I see nothing wrong with using exit() inside functions as long as you know exactly where it exited (when debugging, etc). It would in a sense be equivalent to an assert.
I would use assert/abort for debugging, return in main, and avoid using exit at all. It comes in handy every once in a while, but I would consider heavy use of atexit() and exit() to be indicative of poor code structure.
Hmm... this makes me wonder... could one use exit/atexit combined with setjmp/longjmp to make a poor man's exception handler? Hideous!
Haha. Indeed. I prefer simple goto as poor man's exception handling though. I've seen atexit(SDL_Quit); being used quite a lot, but that's about it for larger programs.
Offline
Pages: 1