Re: Pointer dereference rather than sizeof?



rlb@xxxxxxxxxxxxxxxxxxxxxx (Richard Bos) writes:

Peter Nilsson <airia@xxxxxxxxxxx> wrote:

<http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html>

Quote:

142(S6.5, toward the end): The remark about casting the
return value of malloc ("the proper method is to declare
... then explicitly coerce") needs to be rewritten. The
example is correct and works, but the advice is debatable
in the context of the 1988-1989 ANSI/ISO standards. It's
not necessary (given that coercion of void * to
ALMOSTANYTYPE * is automatic), and possibly harmful if
malloc, or a proxy for it, fails to be declared as
returning void *. The explicit cast can cover up an
unintended error. On the other hand, pre-ANSI, the cast
was necessary, and it is in C++ also.

It's surprising how some people seem to have interpreted
"The example is correct and works" as somehow meaning the
example is incorrect and doesn't.

Not quite as surprising as the number of people who read "needs to be
rewritten" as "is more than correct and should be followed by
everybody".

Richard

What is more surprising is how many people here tell fibs to enhance
their clique status as Hathfield wannabes. The old "I have never seen
that" remarks seen so often are simply lies from anyone who has worked
in industry because so much code is NOT Ansi C. As for the potential for
error from putting the cast in. since the implementation provides the
malloc in 99.9999999999999999999999999999% of instances there really is
not issue since it will be correctly prototyped.
.



Relevant Pages

  • Re: malloc + 4??
    ... >> Providing a prototype of malloc() yourself is arguably not valid ... >> 2 Provided that a library function can be declared without ... >> permissible to declare the function and use it without ... >> including its associated header. ...
    (comp.lang.c)
  • Re: IS this a proper way of freeing memory with free()
    ... remember a few years ago when one of the versions of gcc (can't remember ... Or perhaps the system's stdlib.h didn't declare malloc()? ... it would be very surprising. ...
    (comp.lang.c)
  • Re: Arraym malloc() and free() question
    ... > to any type defined in a header, it is also permissible to declare ... > including its associated header. ... >malloc, even if you had included another header that defined size_t. ... Dan Pop ...
    (comp.lang.c)
  • Re: Comments on my code?
    ... Since you didn't declare the argument type, ... you're passing an argument of type int. ... void *malloc; ... declaration of malloc falls in the same category as f2, ...
    (comp.lang.c)
  • Re: How to read data ?
    ... then explicitly coerce") needs to be rewritten. ... and possibly harmful if malloc, or a proxy for it, ... fails to be declared as returning void *. ... The explicit cast ...
    (comp.lang.c)