itoa'd you so?

Steve smorrey at gmail.com
Wed Sep 19 15:45:15 MDT 2007


Yes Michael you have it correct.
I made the mistake of assuming this would be a return value (copy)
operation, but I can see now it's passing a pointer, and that pointer
is pointing to something which is now out of scope.

Thankfully I looked at some code I actually do have and it uses #1
which is safe because it's returning the value.

On 9/19/07, Michael L Torrie <torriem at chem.byu.edu> wrote:
> 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());
> >>> }
>
>
> /*
> PLUG: http://plug.org, #utah on irc.freenode.net
> Unsubscribe: http://plug.org/mailman/options/plug
> Don't fear the penguin.
> */
>



More information about the PLUG mailing list