473,837 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5286
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.co m> 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.co m> 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.co m> 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
2680
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
11001
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 one rather than the other? Thanks!
5
28063
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
8071
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 situation? Also I was wondering if it whould be wise to combine the standard status with return. exp:
20
3620
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 exit(n)) statement provide both function call and return features of the C programming language? /* headers omitted */ int main (void)
6
15511
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 file.exists(myFile) return 1 'file downloaded else return 0 'file not downloaded
14
21000
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
4074
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
1662
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
3427
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 methods can fail I have to check them to be running ok, like this:
0
9693
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10583
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10638
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10280
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9419
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7824
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3128
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.