You are not logged in.

#1 2010-06-17 20:32:44

Anikom15
Banned
From: United States
Registered: 2009-04-30
Posts: 836
Website

[C] exit vs return

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

#2 2010-06-17 20:48:45

Cyrusm
Member
From: Bozeman, MT
Registered: 2007-11-15
Posts: 1,053

Re: [C] exit vs return

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

#3 2010-06-17 22:16:11

Themaister
Member
From: Trondheim, Norway
Registered: 2008-07-21
Posts: 652
Website

Re: [C] exit vs return

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

#4 2010-06-18 02:48:59

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [C] exit vs return

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! wink

Offline

#5 2010-06-18 16:42:34

Anikom15
Banned
From: United States
Registered: 2009-04-30
Posts: 836
Website

Re: [C] exit vs return

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

#6 2010-06-19 01:21:56

Themaister
Member
From: Trondheim, Norway
Registered: 2008-07-21
Posts: 652
Website

Re: [C] exit vs return

Trent wrote:
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! wink

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

Board footer

Powered by FluxBB