String corrupted after throw

W

William Payne

Hello, I am making a binary tree and I've encountered a problem when I throw
an exception. Consider this code:

/* in inserter function for the tree */
char error_message[64];

sprintf(error_message, "%i already present in the tree.", n); /* n is of
type int */

cout << error_message << endl;

throw error_message;

/* in main() */
try
{
binary_tree.insert(7);
}
catch(const char* error_message)
{
cerr << error_message << endl;
}

When I print out the string I am about to throw in the inserter-function, I
get:
7 already present in the tree.

but when I catch the exception in main() and print out I get:
? al(?C y present in the tre"í

As you can see, it has become corrupted. What am I doing wrong? Must I
allocate the error message dynamically or?

/ WP
 
C

Clark Cox

William Payne said:
char error_message[64];
[snip]

throw error_message;
[snip]

As you can see, it has become corrupted. What am I doing wrong?

You're throwing a pointer that points to a automatic variable local
to the block that is doing the throwing.
Must I allocate the error message dynamically or?

You're throwing the value of the char*, but once you leave this
scope, the array no longer exists, so that dereferencing that pointer
results in undefined behavior. This is the same issue with trying to
return a pointer to a local variable.
Try throwing (and catching) a std::string -- or some subclass of
std::exception instead.
 
A

Alf P. Steinbach

* "William Payne said:
Hello, I am making a binary tree and I've encountered a problem

In the great tradition now explained to me by others, of first providing
a short answer to "the showstopping problem":


* The showstopping problem is that you're throwing a pointer to a local
variable that's gone out of scope when that pointer is used.

* The fix of the showstopping problem is to throw a
std::runtime_error exception, catch a 'std::exception const& x'.

* The main overall problem is C style instead of C++ style, but since
this is a rather subtle point don't think about it, just forget it.


when I throw
an exception. Consider this code:

/* in inserter function for the tree */
char error_message[64];

sprintf(error_message, "%i already present in the tree.", n); /* n is of
type int */

Don't use unsafe C formatting functions.

As a newbie only use type-safe C++ formatting such as e.g.
std::eek:stringstream.



cout << error_message << endl;

throw error_message;

Don't throw anything other than objects of classes derived from
std::exception.


/* in main() */
try
{
binary_tree.insert(7);
}
catch(const char* error_message)
{
cerr << error_message << endl;
}

When I print out the string I am about to throw in the inserter-function, I
get:
7 already present in the tree.

but when I catch the exception in main() and print out I get:
? al(?C y present in the tre"í

As you can see, it has become corrupted. What am I doing wrong? Must I
allocate the error message dynamically or?

No, leave that to std::exception.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top