itoa'd you so?

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


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.

On 9/19/07, Michael L Torrie <torriem at chem.byu.edu> wrote:
> Steve wrote:
> > So I was asked how I would write an itoa function.
> > The goal was to target an embedded platform, and so clock cycles and
> > overhead really count.
> >
> > The solution could be in either C or C++
>
> In the context of a company that does embedded programming, he really
> means "do it in C."
>
> >
> > I came up with 2 solutions which both amounted to roughly the same code.
> >
> > std::string itoa(int in){
> >       std::stringstream out;
> >       out << in;
> >       return(out.str());
>      ^^^^^^^^^^^^^^^^^^^
> nope.  you can't do that.
>
> > }
> >
> > and
> >
> > char* itoa(int in){
> >       std::stringstream out;
> >       out << in;
> >       return(out.rdbuf.c_str());
>     ^^^^^^^^^^^^^^
> can't do that either, at least safely.
>
> > }
> >
> > So my question is, what is wrong with this method (I haven't tested it
> > so there may be a minor syntax error, but that aside)?
> >
> > Seems to me it's the most efficient way to do it in a typesafe manner,
> > but maybe I'm just missing something.
> > Can anyone see anything wrong or maybe just a better way to do an itoa?
> >
> > Sincerely,
> > Steve
> >
> > /*
> > PLUG: http://plug.org, #utah on irc.freenode.net
> > Unsubscribe: http://plug.org/mailman/options/plug
> > Don't fear the penguin.
> > */
> >
>
>
> /*
> 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