itoa'd you so?

Michael L Torrie torriem at
Wed Sep 19 15:29:10 MDT 2007

Michael L Torrie wrote:
>> std::string itoa(int in){
>> 	std::stringstream out;
>> 	out << in;
>> 	return(out.str());
>      ^^^^^^^^^^^^^^^^^^^
> nope.  you can't do that.

Nevermind.  This one would probably work, if out.str() returns a new
object and not a reference to some internal object within the out object.

>> }
>> and
>> char* itoa(int in){
>> 	std::stringstream out;
>> 	out << in;
>> 	return(out.rdbuf.c_str());
>     ^^^^^^^^^^^^^^
> can't do that either, at least safely.

This one, though, is definitely bad.  By the time the caller gets the
char *, it's already pointing to invalidated stack space.  Good way to
get a segfault or worse a buffer overrun that could lead to bad things.

More information about the PLUG mailing list