Re: C99 Question

From: pete (pfiland_at_mindspring.com)
Date: 12/30/03


Date: Tue, 30 Dec 2003 11:39:49 GMT

Vijay Kumar R Zanvar wrote:
>
> Hi,
>
> Which section of C99 says that return value
> of malloc(3) should not be casted?

How about an errata from K&R2 instead ?

142(§6.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.

http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html

-- 
pete


Relevant Pages

  • 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)
  • Re: faq & casting malloc()
    ... The remark about casting the return value ... explicitly coerce") needs to be rewritten. ... fails to be declared as returning void *. ... The explicit cast can cover up an unintended error. ...
    (comp.lang.c)
  • Re: Pointer dereference rather than sizeof?
    ... cast malloc's pointer. ... return value of malloc ("the proper method is to declare ... then explicitly coerce") needs to be rewritten. ... returning void *. ...
    (comp.lang.c)
  • Re: help w/ c/c++ problem
    ... You may also declare mainas void if it does not ... You are therefore free to declare mainas required ... for which this International Standard imposes no requirements ...
    (comp.lang.c)
  • Re: help w/ c/c++ problem
    ... You may also declare mainas void if it does not ... You are therefore free to declare mainas required ... If you read the standard as well as the annotations, ...
    (comp.lang.c)