Stefan said:
It seems to allocate a stack entry
st = (stack_t *) malloc( ...
and then, in the next line
st->base = ...
it seems to use »st« without ever taking account for the
possibility that »st« might be 0?
This might be fine when a program is used by the programmer
himself, who can cope with segment violations or other
strange runtime-behavior. But it might not be appropriate
when software is intended to be delievered as a product to a
customer who needs to depend on it.
You may be right that error checking should be performed. Nonetheless, what's
important in the real world source code (validate everything) isn't necessarily
what's important in a technical or academic publication (illustrate concepts and
convey ideas).
When an author starts to include each and every validation test in a code
snippet example then the example code becomes verbose. As a consequence, the
reader's attention will also be on minor and even irrelevant details and it will
be a bit more difficult to get the gist of what the author tried to communicate
through the example code, all of this while filling page after page of
untestable code. After all, who is going to happily waste their precious time
transcribing pages after pages of source code examples? So, to put it in other
words, if you explicitly include validations in your examples then getting to
the point becomes a little bit less efficient. And needlessly so.
There are authors who understood perfectly the need for succinct source code
examples and also placed a great value on the need to perform validations
thoroughly. One such author was Richard Stevens. In his series of books he
made a point in providing source code examples which were straight to the point
and unencumbered by those long, tedious series of sanity tests, which are pretty
much irrelevant for the topic being discussed. As a result, his explanations
are clear, straight to the point and very easy to understand. Yet, all
validations were kept in the code. The thing is, he achieved that by writing
wrappers for the system calls he used in the examples, which he included in
custom-tailored header files, in effect sweeping the cruft under the rug.
The problem with that approach is that, although it's a blessing for those who
are patiently reading the book from start to finish, not missing a beat, it
represents a small source of problems for those who use the book more as a
reference instead of a casual read. The code snippets don't compile, the
functions being used don't actually exist and some headers are nowhere to be
found. Although that problem can be easily surpassed by RTFB, it still poses as
a problem to clueless newbies.
So, if an author omits a series of irrelevant validations from a book's source
code snippets then that is not a problem, much less a demonstration of how to
write sloppy code. It is, in fact, a great way to both help the reader
understand what the book is actually about and preserve the book's usefulness as
a reference work. It's far better than both diluting your example in a sea of
if() blocks and relying on clever tricks such as wrappers. Therefore the author
should be commended by omitting the tests, instead of being criticized.
And on a side note, testing the return of a malloc() call is terribly basic
stuff, in the sense that if you fail to understand the need for that, even when
missing from the examples, then probably you should be reading a book on basic C
programming before delving into that one.
Rui Maciel