itoa'd you so?

Michael L Torrie torriem at chem.byu.edu
Wed Sep 19 15:25:10 MDT 2007


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




More information about the PLUG mailing list