itoa'd you so?

Michael L Torrie torriem at
Wed Sep 19 15:37:07 MDT 2007

Steve wrote:
> Ok now this has got me stumped what was unsafe about #2?
> Taking advantage of the type safety features in the standard lib, is
> what I was trying to show, and I have a lot of code running using
> both.  If thats unsafe, I have some patchin' to do.

Well, if I understand your code correctly, you're returning a char *
pointer (likely const char *) to the internal buffer of the out
stringstream, correct?  If so, then on return the caller receives a
pointer to the internal buffer of the out object which lived in the
stack frame of the itoa call, which no longer exists.

>>> char* itoa(int in){
>>>       std::stringstream out;
>>>       out << in;
>>>       return(out.rdbuf.c_str());
>>> }

More information about the PLUG mailing list