473,399 Members | 2,159 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,399 software developers and data experts.

std::exit Ambiguity?

An excerpt from the Standard:
4 Calling the function
void exit(int);
declared in <cstdlib> (18.3) terminates the program without leaving the
current block and hence without
destroying any objects with automatic storage duration (12.4). If exit is
called to end a program during
the destruction of an object with static storage duration, the program has
undefined behavior.
Now take the following code:
#include <cstdlib>
#include <iostream>

class Blah
{
public:

~Blah()
{
std::cout << "\nBlah's Destructor!\n";
}
};
int main()
{
Blah blah;

{ exit(0); }
}
I myself would've thought that blah's destructor *would* have been called...
after all, its scope isn't that of "the current block".
Thoughts?
-JKop
Jul 22 '05 #1
10 5017
The destructor doesn't get called in the following either:

#include <cstdlib>
#include <iostream>

void Cow()
{
exit(0);
}

int main()
{
Blah blah;

Cow();
}

Jul 22 '05 #2

"JKop" <NU**@NULL.NULL> wrote in message
news:Qx*******************@news.indigo.ie...
An excerpt from the Standard:
4 Calling the function
void exit(int);
declared in <cstdlib> (18.3) terminates the program without leaving the
current block and hence without
destroying any objects with automatic storage duration (12.4). If exit is
called to end a program during
the destruction of an object with static storage duration, the program has
undefined behavior.
Now take the following code:
#include <cstdlib>
#include <iostream>

class Blah
{
public:

~Blah()
{
std::cout << "\nBlah's Destructor!\n";
}
};
int main()
{
Blah blah;

{ exit(0); }
}
I myself would've thought that blah's destructor *would* have been called... after all, its scope isn't that of "the current block".
Thoughts?


I think you're misreading the standard. It doesn't say that destruction
depends upon whether an object is in the current block or not. It says that
destructors for automatic objects are not called BECAUSE the current block
isn't left. In other words no destructors for automatic objects will be
called, period.

If you want to end a program and have automatic object destructors called
then throw an exception.

john
Jul 22 '05 #3
>> 4 Calling the function
void exit(int);
declared in <cstdlib> (18.3) terminates the program without leaving
the current block and hence without
destroying any objects with automatic storage duration (12.4). If exit
is called to end a program during
the destruction of an object with static storage duration, the program
has undefined behavior.

I think you're misreading the standard. It doesn't say that destruction
depends upon whether an object is in the current block or not. It says
that destructors for automatic objects are not called BECAUSE the
current block isn't left. In other words no destructors for automatic
objects will be called, period.


Hmm... yeah... I suppose... but I'd prefer if the Standard were more
explicit. For example:

without destroying any and all objects with automatic storage, including
those defined in other blocks.
If you want to end a program and have automatic object destructors
called then throw an exception.

john

Helpful as ever, thanks!

You mean throw an exception and not catch in?

What I'm doing is calling a function from main. I want this function to have
the "power" to end the program.
As in:
void SomeFunc()
{
//something goes wrong

throw int();
}
int main()
{
SomeFunc();
}
What happens when an exception isn't caught... I take it there's a certain
"std::" function called, yeah?

But then again, seeing as how there's no destructors to be called in my code
above, is there anything... wrong... with calling exit? And let's say for
instance that you have an "std::string" object with automatic storage, if
you call exit() are you invoking UB by not having its destructor called? And
even though the destructor isn't called, is the memory still deallocated for
the object?
-JKop
Jul 22 '05 #4

<NU**@NULL.NULL> wrote in message
news:aX*******************@news.indigo.ie...
4 Calling the function
void exit(int);
declared in <cstdlib> (18.3) terminates the program without leaving
the current block and hence without
destroying any objects with automatic storage duration (12.4). If exit
is called to end a program during
the destruction of an object with static storage duration, the program
has undefined behavior.

I think you're misreading the standard. It doesn't say that destruction
depends upon whether an object is in the current block or not. It says
that destructors for automatic objects are not called BECAUSE the
current block isn't left. In other words no destructors for automatic
objects will be called, period.


Hmm... yeah... I suppose... but I'd prefer if the Standard were more
explicit. For example:

without destroying any and all objects with automatic storage, including
those defined in other blocks.
If you want to end a program and have automatic object destructors
called then throw an exception.

john

Helpful as ever, thanks!

You mean throw an exception and not catch in?

What I'm doing is calling a function from main. I want this function to

have the "power" to end the program.
As in:
void SomeFunc()
{
//something goes wrong

throw int();
}
int main()
{
SomeFunc();
}
What happens when an exception isn't caught... I take it there's a certain
"std::" function called, yeah?
Not sure, std::terminate I think, look it up. But in practice it might be
better to do this

int main()
{
try
{
SomeFunc();
}
catch (...)
{
}
}

just to make sure that you exit main in the normal way. This should ensure
that global objects get destructed too.

But then again, seeing as how there's no destructors to be called in my co de above, is there anything... wrong... with calling exit? And let's say for
instance that you have an "std::string" object with automatic storage, if
you call exit() are you invoking UB by not having its destructor called?
No I don't think so.
And
even though the destructor isn't called, is the memory still deallocated for the object?


That's system dependent I think. Most O/S will return all allocated memory
to the system upon program exit (after all whoever heard of a program that
didn't leak memory). Windows and Unix both do this.

john
Jul 22 '05 #5
NU**@NULL.NULL wrote:

What I'm doing is calling a function from main. I want this function to have
the "power" to end the program.
As in:
void SomeFunc()
{
//something goes wrong

throw int();
}
int main()
{
SomeFunc();
}
What happens when an exception isn't caught... I take it there's a certain
"std::" function called, yeah?

But then again, seeing as how there's no destructors to be called in my code
above, is there anything... wrong... with calling exit? And let's say for
instance that you have an "std::string" object with automatic storage, if
you call exit() are you invoking UB by not having its destructor called? And
even though the destructor isn't called, is the memory still deallocated for
the object?


The memory will be eventually freed by the operating system (hopefully).

You could always

class ExcTerminate {};

void SomeFunc() {
throw ExcTerminate();
}

int main() {
// ...
try {
// ...
}
catch(ExcTerminate) {
return 0;
}
// ...
}

- J.
Jul 22 '05 #6
> class ExcTerminate {};

void SomeFunc() {
throw ExcTerminate();
}

int main() {
// ...
try {
// ...
}
catch(ExcTerminate) {
return 0;
}
// ...
}

You're a mind reader - I just did that 5 minutes ago!
-JKop
Jul 22 '05 #7
How about:
struct ExcTerminate {
int code;
ExcTerminate(int retcode) : code(retcode){}
};

int main()
{
try {
...
}
catch(ExcTerminate exc){
return exc.code;
}
...
}

Regards,
Vyacheslav

"JKop" <NU**@NULL.NULL> wrote in message
news:Iv*******************@news.indigo.ie...
class ExcTerminate {};

void SomeFunc() {
throw ExcTerminate();
}

int main() {
// ...
try {
// ...
}
catch(ExcTerminate) {
return 0;
}
// ...
}

You're a mind reader - I just did that 5 minutes ago!
-JKop

Jul 22 '05 #8
struct ExcTerminate {
int code;
ExcTerminate(int retcode) : code(retcode){}
};

int main()
{
try {
...
}
catch(ExcTerminate exc){
return exc.code;
}
...
}

Even better!
-JKop

Jul 22 '05 #9
NU**@NULL.NULL wrote:
4 Calling the function
void exit(int);
declared in <cstdlib> (18.3) terminates the program without leaving
the current block and hence without
destroying any objects with automatic storage duration (12.4). If exit
is called to end a program during
the destruction of an object with static storage duration, the program
has undefined behavior.

I think you're misreading the standard. It doesn't say that destruction
depends upon whether an object is in the current block or not. It says
that destructors for automatic objects are not called BECAUSE the
current block isn't left. In other words no destructors for automatic
objects will be called, period.


Hmm... yeah... I suppose... but I'd prefer if the Standard were more
explicit. For example:

without destroying any and all objects with automatic storage, including
those defined in other blocks.


That's what it says. You quoted it yourself: "... and hence without
destroying any objects with automatic storage duration". Nothing about
objects within the current block, just "any objects".
You mean throw an exception and not catch in?

What I'm doing is calling a function from main. I want this function to
have the "power" to end the program.
As in:
void SomeFunc()
{
//something goes wrong

throw int();
}
int main()
{
SomeFunc();
}
What happens when an exception isn't caught... I take it there's a certain
"std::" function called, yeah?

But then again, seeing as how there's no destructors to be called in my
code above, is there anything... wrong... with calling exit? And let's say
for instance that you have an "std::string" object with automatic storage,
if you call exit() are you invoking UB by not having its destructor
called? And even though the destructor isn't called, is the memory still
deallocated for the object?


Depends on the OS, but usually, the memory is freed. But think about other
things, like a file which buffered data has to be written to before being
closed or a database connection that needs to be shut down properly.

Jul 22 '05 #10

<NU**@NULL.NULL> wrote in message news:aX*******************@news.indigo.ie...
What I'm doing is calling a function from main. I want this function to have
the "power" to end the program.


No you have to catch it in main. It's unspecified if the stack is unwound on the way to
terminate.

Jul 22 '05 #11

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

Similar topics

12
by: Siemel Naran | last post by:
(1) About std::exit. If I call this function, will the system call the destructors of all objects in the call stack. Eg. void f() { std::exit(1); } void g() { X x; f(); } Does the call in...
7
by: zbyszek | last post by:
I am working with a large C++ program which, for reasons of backward compatibility, uses C's printf and fprintf rather than iostreams. For a certain type of build I want to provide new functions...
3
by: faz | last post by:
Hai, i have used exit(1) in my code and included conio.h file also but it is giving the following.. error C2065: 'exit' : undeclared identifier pls suggest me
19
by: Gerry Ford | last post by:
I've been pecking away at writing a program that will calculate the inner product of two double-width four-vectors. Larry thinks I'm well started with the following source to populate a vector:...
13
by: MZaza | last post by:
What command can I use to make the program quite after a certain condition? -- Mustafa Zaza
4
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
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
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.