RE: [Dev-C++] itoa - simple implementation [was: integer to string]
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <iv...@em...> - 2003-04-02 10:48:19
|
> -----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
|