473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

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.

Jul 22 '05 #1
4 5264
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
Jul 22 '05 #2
"John Black" <bl***@eed.com> wrote in message
news:41***************@eed.com...
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

Jul 22 '05 #3

"John Black" <bl***@eed.com> wrote in message
news:41***************@eed.com...
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

Jul 22 '05 #4

"John Black" <bl***@eed.com> wrote in message news:41***************@eed.com...
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


Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
2
by: fwee | last post by:
What is the difference between using exit(n) and return n in main(), if any? I know that exit(n) is a function that exits and return n exits via a return value, but is there any reason to specify...
5
by: QQ | last post by:
I know there are many functions that I can exit the program such as return 0, exit(0), exit(1),_EXIT(0) .... What are the difference between them? Thanks a lot!
17
by: jwaixs | last post by:
Hello, I was wondering, what's the difference between exit and return in the main() function? For me they both look the same, or aren't they? And if they aren't, which should I use in which...
20
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this...
6
by: johnnyG | last post by:
Greetings, Since Main() is a subroutine in VB.NET console apps and not a function, is there a way to return a value to a script ro other "caller" for a VB.NET console .exe? sub main() like if...
14
by: tshad | last post by:
Is there any difference between Return and Exit Sub? I have some code that uses both when I have an error in my Sub. Thanks, Tom
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
16
by: jayapal | last post by:
hi all, what is the differrence b/w the usage or return and the exit in the C programming.. thanks, jay
11
by: =?Utf-8?B?Um9nZXIgVHJhbmNoZXo=?= | last post by:
Hello, I have a question about the infamous GOTO statement and the way to return a result from a sub: I have a sub that has to make some calls to external COM methods, and because these...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.