New 'n' Improved comp.lang.c FAQ List

Hendrik Vermooten hendrik at zeusa.UUCP
Sat Apr 6 19:42:35 AEST 1991


In article <1991Apr1.203024.19679 at unlv.edu>, grover at big-joe.cs.unlv.edu (Kevin Grover) writes:
> > [misc deleted. . .]
> > Q: Why doesn't this function work:
> >
> >         char *itoa(int i)
> >         {
> >         static char retbuf[5];         /* biggest int: 32769 */
> >         sprintf("%d", retbuf, i);
> >         return retbuf;
> >         }

WOW!
char retbuf[5];         /* biggest int: 32769 */

Watch it: 5 isn't enough; what about -32767 followed by a '\0': it has
to be char retbuf[7];

Maybe this is a way of teaching beginning C users how C can overwrite the
local stack frame and make a DOS machine freeze up and a UNIX box coredump :-)
-- 
--------------------------------------------------------------------------
Hendrik Vermooten, ZEUS software    TEL +27 12 64-5507, FAX +27 12 64-8382
Bang: ..!uunet!ddsw1!olsa99!zeusa!hendrik     or        hendrik at zeusa.UUCP
--------------------------------------------------------------------------



More information about the Comp.lang.c mailing list