C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

String corrupted after throw

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
William Payne
Guest





PostPosted: Sun Mar 07, 2004 10:30 pm    Post subject: String corrupted after throw Reply with quote



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


Back to top
Clark Cox
Guest





PostPosted: Sun Mar 07, 2004 11:58 pm    Post subject: Re: String corrupted after throw Reply with quote



In article <c2g7on$gkq$1 (AT) news (DOT) island.liu.se>,
"William Payne" <mikas493_no_s_p_a_m_ (AT) student (DOT) liu.se> wrote:

Quote:
char error_message[64];

[snip]

Quote:
throw error_message;

[snip]

Quote:
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.

Quote:
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.


Back to top
Alf P. Steinbach
Guest





PostPosted: Mon Mar 08, 2004 12:05 am    Post subject: Re: String corrupted after throw Reply with quote



* "William Payne" <mikas493_no_s_p_a_m_ (AT) student (DOT) liu.se> schriebt:
Quote:
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.



Quote:
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::ostringstream.




Quote:
cout << error_message << endl;

throw error_message;

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



Quote:
/* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.