Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!bu.edu!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!mcsun!hp4nl!svin02!wsinis03!debra From: debra@wsinis03.info.win.tue.nl (Paul De Bra) Newsgroups: comp.lang.c Subject: Re: New 'n' Improved comp.lang.c FAQ List Message-ID: <1858@svin02.info.win.tue.nl> Date: 3 Apr 91 10:46:03 GMT References: <1991Apr1.203024.19679@unlv.edu> <31946@shamash.cdc.com> <3739@jethro.Corp.Sun.COM> Sender: news@svin02.info.win.tue.nl Reply-To: debra@info.win.tue.nl Organization: Eindhoven University of Technology, The Netherlands Lines: 22 In article <3739@jethro.Corp.Sun.COM> fguille@France.Sun.COM writes: >bls@u02.svl.cdc.com (Brian Scearce) writes: > char *itoa(int i) > { > static char retbuf[5]; /* biggest int: 32768 */ > sprintf(retbuf, "%d", i); > return retbuf; > } >Should'nt the *really really correct* version allocate an extra character >in the retbuf array for the end of string delimiter \0 ??? Actually, this version of itoa is wrong for 2 reasons: - retbuf should have size 7, to account for negative ints as well as the terminating null character. - this version of itoa will not work as expected in a call like: printf("%s %s\n",itoa(2),itoa(3)); the only way to do it right is to allocate memory for the buffer on each call. but then, freeing that memory becomes a problem as it cannot be done inside itoa... Paul. (debra@win.tue.nl)