Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply

C++ - return vs exit

 
Thread Tools Search this Thread
Old 09-08-2004, 08:53 PM   #1
John Black
 
Posts: n/a
Default return vs exit

Hi,
I wonder what is the difference between ending the main() with and
without exit(0)?

My code is like this flow,

class ClassA{
ClassB* cb;

int toExit();

public:
~ClassA();
... constructors and all other API...

}

ClassA::~ClassA(){
this->toExit();
}

ClassA::toExit(){
if (cb != NULL){
delete cb;
cb = NULL;
}
}

int main(int argc, char** argv){
ClassA ca;

... some operation ...

return 0;
}

Then the program exits with a core dump, in debugger I can see that
the core dump comes from "}" after "return 0;" in main() and "delete cb"
in ClassA::toExit().

In purify it says some FMR, Free Memory Read errors.

Then I replace "return 0" with "exit(0)" in main(), everything is
fine: there is no core dump and purify FMR errors are gone!

What's the trick here?

Thanks.

  Reply With Quote
Old 09-08-2004, 09:08 PM   #2
Phlip
 
Posts: n/a
Default Re: return vs exit

John Black wrote:

> I wonder what is the difference between ending the main() with and
> without exit(0)?
>
> My code is like this flow,
>
> class ClassA{
> ClassB* cb;
>
> int toExit();
>
> public:
> ~ClassA();
> ... constructors and all other API...
>
> }


What does your constructor do to cb? Does it ever point to a 'new'ed heap
object?

>
> ClassA::~ClassA(){
> this->toExit();
> }
>
> ClassA::toExit(){
> if (cb != NULL){
> delete cb;
> cb = NULL;
> }
> }
>
> int main(int argc, char** argv){
> ClassA ca;
>
> ... some operation ...
>
> return 0;
> }
>
> Then the program exits with a core dump, in debugger I can see that
> the core dump comes from "}" after "return 0;" in main() and "delete cb"
> in ClassA::toExit().
>
> In purify it says some FMR, Free Memory Read errors.
>
> Then I replace "return 0" with "exit(0)" in main(), everything is
> fine: there is no core dump and purify FMR errors are gone!


Right. exit() is not magic, it is C, without (much) C++ awareness. It
bypasses the destructor for ca, so that never tries to delete cb.

After you fix your bug, never call exit() again. All C++ programs should
find their way back to main()'s closing bracket.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


  Reply With Quote
Old 09-08-2004, 09:12 PM   #3
Unforgiven
 
Posts: n/a
Default Re: return vs exit

"John Black" <> wrote in message
news:...
> Hi,
> I wonder what is the difference between ending the main() with and
> without exit(0)?
>
> Then the program exits with a core dump, in debugger I can see that
> the core dump comes from "}" after "return 0;" in main() and "delete cb"
> in ClassA::toExit().


First of all, the dump is obviously because cb was never allocated but still
deleted. If you're going to use NULL to check whether it's allocated or not,
you should make sure it's also actually allocated.

> Then I replace "return 0" with "exit(0)" in main(), everything is
> fine: there is no core dump and purify FMR errors are gone!


Indeed. This is because exit() is a function that never returns. As such,
main() never reaches the end, and thus doesn't unwind its stack. As such,
the ClassA destructor is not called, and the offending code is never
reached.

--
Unforgiven

  Reply With Quote
Old 09-08-2004, 09:31 PM   #4
Howard
 
Posts: n/a
Default Re: return vs exit


"John Black" <> wrote in message
news:...
> Hi,
> I wonder what is the difference between ending the main() with and
> without exit(0)?
>
> My code is like this flow,
>
> class ClassA{
> ClassB* cb;
>
> int toExit();
>
> public:
> ~ClassA();
> ... constructors and all other API...
>
> }
>
> ClassA::~ClassA(){
> this->toExit();
> }
>
> ClassA::toExit(){
> if (cb != NULL){


You don't need to check for NULL here. Calling delete on a pointer whose
value is NULL has no effect (good OR bad).

BUT!... you need to either set cb to NULL in your constructor, or make sure
it is assigned a valid value (using new) before this function is ever
called.

But why do you have a "toExit" function at all? Why not just delete the
object inside your destructor? (If you're going to be creating and deleting
the object more than once, you might want to rename it to something more
meaningful, such as "DeleteB".)

> delete cb;
> cb = NULL;
> }
> }
>
> int main(int argc, char** argv){
> ClassA ca;
>
> ... some operation ...
>
> return 0;
> }
>
> Then the program exits with a core dump, in debugger I can see that
> the core dump comes from "}" after "return 0;" in main() and "delete cb"
> in ClassA::toExit().
>
> In purify it says some FMR, Free Memory Read errors.
>
> Then I replace "return 0" with "exit(0)" in main(), everything is
> fine: there is no core dump and purify FMR errors are gone!


Don't cal exit. It's not solving your problem, just hiding it!

>
> What's the trick here?
>
> Thanks.


-Howard



  Reply With Quote
Old 09-09-2004, 04:04 AM   #5
Alex Vinokur
 
Posts: n/a
Default Re: return vs exit


"John Black" <> wrote in message news:...
> Hi,
> I wonder what is the difference between ending the main() with and
> without exit(0)?
>

[snip]

Behavior of the program with return, exit () and abort () is not the same one.

Look at
http://groups.google.com/groups?selm...1.dejanews.com

--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn




  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
DVD Verdict reviews: DAIMAJIN / RETURN OF DAIMAJIN / WRATH OF DAIMAJIN and more! DVD Verdict DVD Video 0 05-24-2005 08:13 AM
DVD Verdict reviews: THE BLUE LAGOON AND RETURN TO THE BLUE LAGOON DOUBLE FEATURE and more! DVD Verdict DVD Video 0 03-18-2005 08:14 AM
DVD Verdict reviews: THE LORD OF THE RINGS: THE RETURN OF THE KING: SPECIAL EXTENDED EDITION and more! DVD Verdict DVD Video 0 01-25-2005 09:34 AM
DVD Verdict reviews: THE LORD OF THE RINGS: THE RETURN OF THE KING and more! DVD Verdict DVD Video 0 06-07-2004 09:05 AM
DVD Verdict reviews: BARBIE OF SWAN LAKE, THE RETURN OF SWAMP THING, and more! DVD Verdict DVD Video 0 11-22-2003 09:04 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47