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