RE: [Dev-C++] itoa - simple implementation [was: integer to string]
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-04-02 18:10:37
|
For a itoa() that returns a pointer, you normally want it defined similar
to asctime(), ctime(), gmtime(), localtime(), ...
If you then need multiple calls before doing something else, you may then
always do:
strcpy(buf1,itoa(i1));
strcpy(buf2,itoa(i2));
...
A dynamically allocating itoa() would be very hard to use in an efficient
way without memory leaks.
You normally don't need to call itoa multiple times before using the
results, since the majority use of itoa() is for output, and if you
have printf(), the following code is meaningless:
char *p1 = itoa(i1), *p2 = itoa(i2);
printf("The result was %s and %s\n",p1,p2);
itoa() is often used in embedded systems where you might not have room
for printf(), and then your output function doesn't support variable
arguments, i.e. you have to do something like:
send(COM1,"The result was ");
send(COM1,itoa(i1));
send(COM1," and ");
send(COM1,itoa(i2));
send(COM1,"\n");
However, the normal implementation of itoa() is with a pointer to buffer
as the second parameter, and possible a number base as third parameter.
Then you have the best of two worlds. Reentrant, quick, non-leaking, ...
/Per W
On Wed, 2 Apr 2003, Ioannis Vranos wrote:
> > -----Original Message-----
> > From: dev...@li...
> > [mailto:dev...@li...] On Behalf
> > Of Luigi Santillo
> > Sent: Wednesday, April 02, 2003 9:01 AM
> > To: dev...@li...
> > Subject: [Dev-C++] itoa - simple implementation [was: integer
> > to string]
> >
> >
> > On Wed, Apr 02, 2003 at 04:38:41AM +0200, Per Westermark wrote:
> > > Assign of string termination will be one position off, i.e.
> > outside your
> > > buffer.
> > > [..]
> >
> > Thanks again for pointing out the bug, I'll fix it as shown by the
> > few code lines below.
> >
> > One thing I was wondering is: since "buf" is static, may I assume
> > it is filled with '\0' by the compiler? (So there will be ne need
> > for me to place the terminating '\0')
> >
> >
> > itoa.c
> > ------
> > /* .. */
> >
> > /* old code: #define MAXSZ 21 */
> > #define MAXSZ 20 /* assuming integer is made of 19 digits + sign */
> >
> > char *
> > itoa(int n)
> > {
> > /* old code: static char buf[MAXSZ]; */
> > static char buf[MAXSZ + 1];
> > char *p = buf + MAXSZ;
> > /* ... */
> > *p = '\0'; /* << really needed ? >> */
> > }
>
>
> This code is not good. Assume this:
>
>
> char *p=itoa(9), *q=itoa(10);
>
>
> I suggest to return a pointer on memory on the free store.
>
>
>
>
> 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
>
|