Re: very much new to c need ur help
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Tue, 20 Nov 2007 21:19:06 -0500
Charlie Gordon wrote:
"CBFalconer" <cbfalconer@xxxxxxxxx> a écrit:.... snip ...
Bien. This I can discuss. The point in that code is to process
the char. sequence incoming. In the process it is necessary (once)
to detect error/eof by picking out the special EOF character.
Absent that, it is necessary to have unsigned char values to
satisfy the requirements of such routines as isdigit(), etc. It
also avoids the evils of mixing unsigned and signed values in
comparisons, for the range check.
Let's discuss it then.
I'll quote your proposed code again, to document this debate:
The cleanest way is to input chars until you have something that
will not fit. For example:
unsigned int rdvalue(FILE *f) {
unsigned int ch, value, err;
value = err = 0;
while (EOF != (ch = getc(f))) {
You are indeed mixing signed and unsigned int in this comparison,
something you qualify as evil, which is also my opinion.
No, this is an equality check, not a comparison. The evils never
arise.
/* ch contains an int, which is the value of the char */
if (!isdigit(ch)) break; /* Only interested in digits */
You are passing an unsigned int to isdigit. isdigit is supposed to receive
an int, with specific constraints on its value. I'm not sure if the
You just may have a point here. My argument is that the value is
known to be positive, and by definition the unsigned and int
representations of positive int values are identical. EOF is never
passed.
.... snip ...
Consider for example the following classic implementation of isdigit:
#define EOF (-1)
extern unsigned char __ctype_flags[UCHAR_MAX + 2];
#define __C_DIGIT 1
#define isdigit(c) (__ctype_flags[(c) + 1] & __C_DIGIT)
On platforms where size_t is larger than unsigned int (both windows
and linux 64 bit), invoking isdigit with (unsigned)EOF will invoke
undefined behaviour.
But you forget that isdigit expects to receive an int, that EOF has
been removed, and that unsigned ints have the identical
representation as positive ints.
It is therefore not advisable to store fgetc(fp) in an unsigned int before
testing for EOF.
NIT - I specifically use getc, so that it can be a macro invocation
and thus can be much quicker than a system function call.
ch = ch - '0'; /* form digit value */
if (((UINT_MAX - ch) / 10) > value) {
This test is incorrect, as was explained in another thread.
I'm not bothering to check it out now, but unsigned int is needed
for proper range checking. I think the only fault is the
replacement of > with <.
/* overflow detected, decide what to do */
ch = ch + '0'; /* restore char value */
break;
}
value = 10 * value + ch;
}
ungetc(ch, f); /* keep exit char for the user */
Same problem as above with the value EOF: converting it back to
int as expected by ungetc() may cause overflow. It would be
better to not call ungetc with the value EOF at all, for example
invoking it in the loop, before the break statements.
No problem with EOF, because if getc returned EOF the subtraction
(and addition) of '0' does not occur.
return err;
} /* untested - you check it out */
untested indeed.
Which is why I added the comment :-)
Thus I maintain the single use in the EOF check is optimum in
clarity. Note that it totally avoids casts.
I obviously disagree: the code is not optimum in clarity, it is
not conforming, you avoid casts, but rely on implicit conversions
with potential overflows...
I trust I have answered your objections. I think everything is
covered.
--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com
.
- Follow-Ups:
- Re: very much new to c need ur help
- From: Charlie Gordon
- Re: very much new to c need ur help
- References:
- very much new to c need ur help
- From: cool_ratikagupta@xxxxxxxxxx
- Re: very much new to c need ur help
- From: Joe Wright
- Re: very much new to c need ur help
- From: sweet rati
- Re: very much new to c need ur help
- From: Joachim Schmitz
- Re: very much new to c need ur help
- From: Mark McIntyre
- Re: very much new to c need ur help
- From: Joachim Schmitz
- Re: very much new to c need ur help
- From: Keith Thompson
- Re: very much new to c need ur help
- From: Richard Heathfield
- Re: very much new to c need ur help
- From: Keith Thompson
- Re: very much new to c need ur help
- From: Charlie Gordon
- Re: very much new to c need ur help
- From: CBFalconer
- Re: very much new to c need ur help
- From: Charlie Gordon
- Re: very much new to c need ur help
- From: CBFalconer
- Re: very much new to c need ur help
- From: Charlie Gordon
- very much new to c need ur help
- Prev by Date: Re: Where are the program variables stored in memory(text, data or stack segments)
- Next by Date: Re: very much new to c need ur help
- Previous by thread: Re: very much new to c need ur help
- Next by thread: Re: very much new to c need ur help
- Index(es):
Relevant Pages
|