Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!garfield!matthew1 From: matthew1@garfield.cs.mun.ca (Matthew J. Newhook) Newsgroups: comp.lang.c Subject: Re: New 'n' Improved comp.lang.c FAQ List Message-ID: <1991Apr5.145533.11376@garfield.cs.mun.ca> Date: 5 Apr 91 14:55:33 GMT References: <910401.0xf001@etiquette.uu.net> <1991Apr1.203024.19679@unlv.edu> <31946@shamash.cdc.com> <1991Apr4.024300.19969@garfield.cs.mun.ca> <030525.19985@timbuk.cray.com> Organization: CS Department, Memorial University of Newfoundland Lines: 124 paul@sequoia.cray.com (Paul Dow (CRI-UK)) writes: >Will this never end..... Here's my three ha'pence.... >In article <1991Apr4.024300.19969@garfield.cs.mun.ca>, matthew1@garfield.cs.mun.ca (Matthew J. Newhook) writes: [ stuff deleted .. ] >|> Actually here is a correct (portable) version (assuming that you want to >|> keep a static character buffer, and not use a newly malloc'd one each time). >|> >|> #include >|> >|> char *itoa(int i) >|> { >|> static int first = 1; >|> static char *s; >|> >|> if (first) { /* IF FIRST TIME ALLOCATE MEMORY FOR STRING */ >|> first = 0; >|> s = (char *) >|> malloc((long) ceil(log10(pow(2, (double) sizeof(int) * 8))) +1); >|> if (!s) { >|> fprintf(stderr,"malloc failed\n"); >|> exit(1); >|> } >|> } >|> >|> sprintf(s, "%d", i); >|> return s; >|> } >|> >|> This is portable, 'cause it always assigns the correct value to the size of >|> the array, without depending on the sizeof(int) == 2. >|> >|> Matthew Newhook >|> >It is easy to say that things are "correct" & "portable". How often have >people been flamed in this newsgroup (and others) for that. A lot of people >are put of replying to questions for fear of being flamed. Mind you, if >the replies were framed as suggestions as opposed to "correct"/"portable" etc, >then they would not irritate the "flamer" so much. >Anyhow, enough of that sideline, let us look at the last example above and ask >a few questions ? >Q1) The "*8" I presume is for the numbers of bits in a byte. Are there > problems here on a 9 bit machine? I don't have access to any such machines, so I cannot test this out. >Q2) Should the first parameter to pow() be a double - the compiler may not > support prototypes. I'm using ANSI C, and the standard says pow takes double. >Q3) The "+1" appears to be for the terminating NULL character. Has any > space being allowed for a leading "-" sign ? woops... >Q4) Malloc is being passed a "long" type for the number of bytes. Should > this be an unsigned (int) [it is on my SunOS 4.1 system]. Should actually be size_t according to the standard. >Q5) If the malloc fails, then a string is written to stderr. Is this > accetable? Is it also acceptable to exit at that point ? > This may not be acceptable to some applications which might not support a > stderr per se ["stderr" might exist, but is it valid to use it ?]. > I have encountered this problem with assrted C libraries on several > systems. It was acceptable for this example... > I have to admit that it is good to see return values checked. Code that > ignores return values is the bane of my life. >Suggestions: >Here are a few things that I would think about. They would not change the functionality, but may be worth considering - depending on ones viewpoint. >S1) I would tend to use a NULL/not NULL test for the pointer to determine if > the buffer needed to be allocated. This is a moot point, IMHO. "if (!p)" is a common enough expression... >S2) It took a bit of thinking about to work out what the > malloc((long) ceil(log10(pow(2, (double) sizeof(int) * 8))) +1); > was doing. Can this be simplified ? I don't know can it? Any ideas anyone? >S3) It took me a second or two to work out where the result of the malloc was > going. It would have helped me of the malloc line was indented more. > Possibly, but where else was the malloc going? Actually, I think someone posted a better solution to mine later... It went like this, if your limits.h defines INT_MIN in the right manner... (ie. -2147483648 instead of (1 << 31) or whatever). And, of course, you use ANSI C. #include #define QUOTE(a) #a char *itoa(int i) { static char retbuf[sizeof(QUOTE(INT_MIN)]; ^^^^^^^^ shouldn't there be a +1 here for the null char? /* * I *think* the above is a legitimate use of sizeof. Can * any standard gurus confirm or deny this? */ (void) sprintf(retbuf, "%d", i); return retbuf; } Aid. (adrian@gonzo.mti.com) >Paul. >[All disclaimers may be assumed] Matthew Newhook -- ----------------matthew1@garfield.cs.mun.ca "Living in the limelight; the universal dream for those who wish to seem. Those who wish to be must put aside the alienation, get on with the facination, the real relation, the underlying theme" - Rush