RE: [Dev-C++] itoa - atoi : another implementation
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-04-02 18:14:38
|
You might have been in even more hurry :-)
How many characters do you allocate if the integer was zero, or negative?
And then of course - itoa() with a sprintf() is of very little use, since
it doesn't solve reentrancy problems, doesn't support variable number
base, and doesn't allow linking without the formatted RTL code.
It isn't even a good optimization of number of source code
characters, since each call MUST be either:
free(itoa(i1));
which is an expensive NOP instruction, or:
char *p = itoa(i1);
use_string(p);
free(p);
which isn't much shorter than
char buf[20];
sprintf(buf,"%d",i1);
use_string(buf);
Whith a static buffer in itoa() you get code length improvement
with:
use_string(itoa(i1));
and with a semi-standard implementation of itoa you get code length
improvement AND extra number bases:
char buf[30];
use_string(itoa(i1,buf,3)); // trinary output
/Per W
On Wed, 2 Apr 2003, Ioannis Vranos wrote:
> In my hurry I forgot the basics:
>
>
>
> > -----Original Message-----
> > From: dev...@li...
> > [mailto:dev...@li...] On Behalf
> > Of Ioannis Vranos
> > Sent: Wednesday, April 02, 2003 2:46 PM
> > To: 'Dev-C++'
> > Subject: RE: [Dev-C++] itoa - atoi : another implementation
> >
> >
> > My own contribution to this entirely unneded function:
> >
> > #include <stddef.h>
> > #include <stdlib.h>
> > #include <stdio.h>
> >
> >
> > char *itoa(int i)
> > {
> > size_t Length;
> > char *p;
> > int temp=i;
> >
> > for(Length=0; temp/=10; Length++)
> > ;
> >
> > p=malloc((Length+1)*sizeof(i));
>
> if(!p)
> return NULL;
>
>
> >
> > sprintf(p, "%d", i);
> >
> > return p;
> > }
> >
> >
> >
> > Why would anyone need itoa()? With sprinf() one can easily do the
> > conversion.
> >
> >
> > sprintf(s,"%d", i);
> >
> >
> > Ioannis Vranos
> >
> > * Programming pages: http://www.noicys.freeurl.com
> > * Alternative URL: http://run.to/noicys
>
> >
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: ValueWeb:
> Dedicated Hosting for just $79/mo with 500 GB of bandwidth!
> No other company gives more support or power for your dedicated server
> http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|