Re: Malcolm's new book - Chapter 1 review
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Tue, 21 Aug 2007 13:19:00 +0000
Philip Potter said:
Richard Heathfield wrote:
<snip>
getc would be an improvement over fgetc, and I'd rather see the EOF
test within the loop control, but again, neither of these is strictly
a correction.
Why is getc() an improvement over fgetc()? I've heard this stated but
never explained.
pete has addressed this question already.
<snip>
nread++;
if (nread == buffsize - 1) {
This is correct, but isn't good C. Idiomatically,
if(++nread == buffsize - 1) {
is better C.
Where in the C standard does it say this?
Section 42.4.2$4(2).
To ask the same thing less confrontationally, why is this better?
Well, "better" is in the eye of the beholder, of course, but for me it's
a question of using an idiom versus not using an idiom.
I have been using C on and off for a number of years, and I would
probably consider myself "average" and certainly not as good as many
regulars on clc. I can read C expressions such as *to++ = *from++ well
because they are idiomatic;
Quite so. As a C programmer, one ought to familiarise oneself with the
ideas of pre- and post-increment.
however, the if statement you stated is
not so idiomatic that I recognise it immediately. I therefore find the
2-line version easier to read.
Oddly, I found it harder to read! Still, that's folks for you. Never so
happy as when we're complaining...
<snip>
In any case, I don't think this criticism of MM's book is particularly
noteworthy in amongst some of the more substantial claims being thrown
around.
Oh, I agree - this is all minor league stuff. But it's the code that was
in front of me at the time. I agree that there wasn't very much wrong
with it.
if (!temp) {
free(buff);
return 0;
}
This code gives up too easily. If you can't get a big wodge of extra
space, try for a smaller wodge. And still a smaller one, in a loop
that decreases the demand in some sensible way. All you really *need*
at this point is one extra character.
Would you necessarily include this extra code in an algorithms book?
There is no need to put *any* code in an algorithms book. But if you're
*going* to include the code, include the right code - code that works,
implements the algorithm properly, handles errors appropriately, and is
robust in the face of straitened resource circumstances.
I
haven't made up my mind; on the one hand, I think a comment afterwards
about this would suffice, but on the other hand if you're
reimplementing fgets() then it's definitely a C task and should be
done in an impeccable C manner.
And that's basically what he's trying to do, except that he's trying to
improve on fgets (by making it possible to read arbitrarily long lines
in a single call) - but failing, IMHO. His poor design and poor
implementation doom the attempt from the start.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.
- Follow-Ups:
- Re: Malcolm's new book - Chapter 1 review
- From: Malcolm McLean
- Re: Malcolm's new book - Chapter 1 review
- From: Philip Potter
- Re: Malcolm's new book - Chapter 1 review
- References:
- Re: Malcolm's new book
- From: Kelsey Bjarnason
- Re: Malcolm's new book
- From: Malcolm McLean
- Re: Malcolm's new book
- From: Kelsey Bjarnason
- Re: Malcolm's new book
- From: Malcolm McLean
- Re: Malcolm's new book
- From: Kelsey Bjarnason
- Re: Malcolm's new book
- From: Richard Heathfield
- Re: Malcolm's new book
- From: Kelsey Bjarnason
- Re: Malcolm's new book
- From: Malcolm McLean
- Re: Malcolm's new book - Chapter 1 review
- From: Eric Sosman
- Re: Malcolm's new book - Chapter 1 review
- From: Malcolm McLean
- Re: Malcolm's new book - Chapter 1 review
- From: Richard Heathfield
- Re: Malcolm's new book - Chapter 1 review
- From: pete
- Re: Malcolm's new book - Chapter 1 review
- From: Richard Heathfield
- Re: Malcolm's new book - Chapter 1 review
- From: Philip Potter
- Re: Malcolm's new book
- Prev by Date: Multiple malloc in the same pointer
- Next by Date: Re: Multiple malloc in the same pointer
- Previous by thread: Re: Malcolm's new book - Chapter 1 review
- Next by thread: Re: Malcolm's new book - Chapter 1 review
- Index(es):
Relevant Pages
|