[re casting malloc/calloc]
It is good practice (read elsewhere in this group for the various arguments
made by others far more qualified than me) to do this.
Perhaps you should read those threads again. There were, as I recall,
a grand total of two qualified advocates of casting malloc/calloc in
the most recent tussle over this issue. One, P. J. Plauger, suggested
that the cast was appropriate in certain situations; this is not one
of them. The other, Sidney Cadot, makes an interesting case, but even
he admits the danger of omitting stdlib.h; he just feels that modern
implementations should flag that anyway, eliminating the need to catch
it by not casting malloc/calloc. He's recommending a different trade-
off, in other words.
How you derive "good practice" from that is beyond me. If you believe,
for some unimaginable reason, that casting malloc/calloc is "good
practice", fine; but vague references to recent threads in c.l.c to
support your claim is bogus.
Notice I said
'should' and not 'must'! Just out of interest, what bugs will it hide?
Other posters have already noted the first one: it hides the failure
to include stdlib.h and have a prototype for calloc in scope.
It also hides the fact that the code invokes undefined behavior,
because it creates a pointer from an int. And yes, there are
conforming hosted C implementations in use where you cannot convert
a pointer to an int and back again successfully. One is being
discussed in another thread right now.
Good
programming practice suggests it is always a good idea to be explicit when
coding rather than implicit (such as in this case an explicit cast rather
than an implicit one).
I believe good programming practice suggests it is always better to
be right than to be wrong. This code was wrong, and your "fix" to it
was wrong.
Advocate casting calloc/malloc if you like. Don't have much hope of
converting the c.l.c majority to your position, though, and please
don't claim that we're already there.
--
Michael Wojcik (e-mail address removed)
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
-- David Bonde