C
CoolPint
Standard exception classes in C++ have what() which returns const char
* and
they have constructors accepting string.
Where is that string created and when is the string destroyed?
In the function below, e is a local object so when the function
terminates,
the internal string should be gone too, isn't it?
But doing "cout << exTest();" in another function still prints the
correct string!
const char * exTest()
{
std:ut_of_range e("Testing Testing Testing Testing Testing");
// cout << &e << endl;
// cout << (void *) e.what() << endl;
return e.what();
}
This makes me wonder where Exception objects are created and when they
are destroyed. Am I just lucky to access deallocated memory space
which has not been overwritten? Or are Exception objects are created
in a special space (Not stack, not heap).
If I am going to write my own exception classes with string messages,
where should I create the string message? When should I deallocate the
space? What if I get bad::alloc during the allocation of space for the
string?
How can standard exception clasess constructors guarantee throw()
specification?
I would very much appreciate any kind explanation on this matter. TIA
* and
they have constructors accepting string.
Where is that string created and when is the string destroyed?
In the function below, e is a local object so when the function
terminates,
the internal string should be gone too, isn't it?
But doing "cout << exTest();" in another function still prints the
correct string!
const char * exTest()
{
std:ut_of_range e("Testing Testing Testing Testing Testing");
// cout << &e << endl;
// cout << (void *) e.what() << endl;
return e.what();
}
This makes me wonder where Exception objects are created and when they
are destroyed. Am I just lucky to access deallocated memory space
which has not been overwritten? Or are Exception objects are created
in a special space (Not stack, not heap).
If I am going to write my own exception classes with string messages,
where should I create the string message? When should I deallocate the
space? What if I get bad::alloc during the allocation of space for the
string?
How can standard exception clasess constructors guarantee throw()
specification?
I would very much appreciate any kind explanation on this matter. TIA