Re: Malcolm's new book - Chapter 1 review



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
.



Relevant Pages

  • Re: The Seed
    ... Some people get the word correction mixed up with bashing. ... ignorant and believing a ball face lie. ... from eternal hell. ...
    (talk.origins)
  • Re: The Seed
    ... < Bo Derek ruined my life! ... Some people get the word correction mixed up with bashing. ... from eternal hell. ...
    (talk.origins)
  • Re: Ex-President for Sale
    ... I think that things like blogs will have a tremendous impact on history. ... They are a blog only in that facts may be immediately disseminated. ... such that the initial story makes the splash, not the correction. ...
    (soc.culture.jewish.moderated)
  • Re: Freestyle Glucose Meter: Approximate Error Correction?
    ... But as you say, the correction ... The user than has to apply two conversions to change ... I thnk there's an AccuChek meter that reads code from the strip. ...
    (misc.health.diabetes)
  • Re: Getting Started With C#
    ... A large part of the issue was all of the "correction" that I ... If it is acceptable that whatever you write the first time ... require a significant time investment to become proficient in). ...
    (microsoft.public.dotnet.languages.csharp)