473,385 Members | 1,564 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,385 software developers and data experts.

Where is Exception Object's internal data stored?

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::out_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
Jul 22 '05 #1
2 2404
"CoolPint" <co******@yahoo.co.uk> wrote...
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?
The Standard says that it's implementation-defined. Most likely it's
a constant NTBS, created when the program is loaded and never destroyed
(or, destroyed along with the program).
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!
It is unknown how std::out_of_range stores the information internally.
Well, not unknown, but implementation-defined. If you must learn how
it does that, look in the source code for your copy of the library.

const char * exTest()
{
std::out_of_range e("Testing Testing Testing Testing Testing");
Here a temporary string is constructed from a [permanently alive] string
literal and passed to 'e'. For all we know (or care), the string literal
is treated as 'const char*' and stored indirectly in the 'string' object.
The internal data of 'std::logic_error' also stores the pointer instead
of copying the string, for example. Reference-counted implementations of
std::string are allowed.
// cout << &e << endl;
// cout << (void *) e.what() << endl;
return e.what();
For all we know, the stored 'std::string' object is now queried for its
'c_str()' and the original pointer (to the string literal) is obtained,
thus resulting in a pointer to something that is never destroyed.
}
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).
See above. It doesn't matter where the exceptions are created.

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?
Those are good questions. You should probably read about implementing
your own exceptions in "The C++ Standard Library". Page 30 contains very
straight-forward recommendations.
How can standard exception clasess constructors guarantee throw()
specification?


They only use mechanisms that are known to never throw, I suppose.

HTH

V
Jul 22 '05 #2

"CoolPint" <co******@yahoo.co.uk> wrote in message
news:15**************************@posting.google.c om...
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?
It will be a member of the exception class created when the exception class
is constructed
and destroyed when it is 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?
What do you mean by 'gone'.
The standard definition is that it is undefined behaviour to use the pointer
that used
to point to it.

But doing "cout << exTest();" in another function still prints the
correct string!
If you use a debug memory allocation library you will find that it doesn't.
It is only there because the memory hasn't been reused for anything else
yet.

const char * exTest()
{
std::out_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?
Yes
Or are Exception objects are created
in a special space (Not stack, not heap).
No - or at least not when used as in your example.
There is nothing magic about the exception classes - they are just plain old
classes
that you could write yourself.

The magic comes when you throw an object (any object) and even
then it wouldn't justify what you are doing which would be the equivalent
of:
const char* foo()
{
try
{
throw out_of_range("shkhsdhqsklhdlk");
}
catch( exception& e)
{
return e.what(); // still undefined but may show the same
behaviour
}
}

The exception will be constructed in 'a special place' but the c-string in
it will still be on the heap as for any other std::string chars.
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
Use a std::string member and it will all happen by magic.
space? What if I get bad::alloc during the allocation of space for the
string?
I bet you £1000000 it will never happen (not counting deliberately
trying to do it) and even if it did you're doomed anyway.
How can standard exception clasess constructors guarantee throw()
specification?

They haven't got one in my copy of the std - except for the ones that don't
take a std::string
in which case they obviously just use a static const char[].
I would very much appreciate any kind explanation on this matter. TIA

Jul 22 '05 #3

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

Similar topics

9
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value...
4
by: Frank Rizzo | last post by:
Hello. Every now and then (it's rare), i get an exception when calling SqlDataAdapter.Fill. The code is calling a stored procedure and attempts to stuff the results into a dataset. It yields the...
28
by: jakk | last post by:
Hello All, I have a question about how to handle exceptions. Iam working on an ASP.NET application which has a presentation layer, Business Layer and DataAccess Layer. Iam confused about where...
9
by: RalphTheExpert | last post by:
I'm getting different behavior if my code is running under the debugger or not. I have modified Winmain to look like this: // Copyright (C) 2002 Microsoft Corporation // All rights reserved....
4
by: Terry | last post by:
Hello, I am trying to get a response for an .aspx page in my current project (same virtual directory) by using WebRequest.GetResponse but I keep getting a exception with "500 Internal server...
5
by: Sridhar | last post by:
Hi, I have created a web application. I have a data access dll to interact with database. I added this dll as a reference to the web application. Now let's say if I am calling a function in data...
10
by: Paul Cheetham | last post by:
Hi, I am developing an application that needs to store some machine-specific settings. The application is going to be published on the network in order to keep the clients on the latest version....
6
by: Liming | last post by:
Hi, In a typical 3 tier model (view layer, busines layer and data access layer) where do you handle your exceptions? do you let it buble up all the way to the .aspx pages or do you handle it in...
3
by: Mike | last post by:
Hi I have problem as folow: Caught Exception: System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Request for the permission of type...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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...

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.