Casting malloc() in ANSI C?

Kevin_P_McCarty at cup.portal.com Kevin_P_McCarty at cup.portal.com
Wed Dec 13 19:51:20 AEST 1989


This is a question on recommended style.

In ANSI C, assigning a void pointer to any pointer variable is
legal, so casting the value returned by malloc() to the
appropriate pointer type is no longer necessary.  As a matter of
recommended style, should the redundant cast be retained?

Casting malloc() is de rigueur in pre-ANSI environments.  Lint
complains about suspicious pointer conversions when casts are not
used.  This made a virtue of necessity by training the programmer
to rely less on automatic conversions (which is a minefield for
the beginner), and to document "possibly doubtful" constructions.

Variants:

        p = (type *) malloc(sizeof(type));
        p = (type *) malloc(sizeof(*p));    /* except with void *p */
        p = malloc(sizeof(type));
        p = malloc(sizeof(*p))              /* except with void *p */

With an ANSI compliant compiler, the cast is unnecessary.  There
are arguments pro and con for continuing the casting style.

Arguments against casting:

- When the appropriate header is included, casting the result
of malloc is unnecessary.  A cast is just redundant clutter;
mention of the type of variable being malloc'ed occurs with
sizeof.

- When the appropriate header is ommitted, a cast will suppress a
helpful diagnostic, and at worst can result in the return value
from malloc() being silently munged beyond use.

Arguments for:

- Forgetting to include header files now seems to me to be the
kind of sloppy lapse of discipline that "forgetting" to declare
variables is.  ("What?  You didn't include header files?  Take
your code back and clean it up, and come back when you have a
*real* problem!"  :-)).  A more common kind of error (for me at
least) is

        p = (type_A *) malloc(n * sizeof(type_A));

but p was declared as (type_B *).  Omitting the cast will hide
this error.  This argument hinges on what is the most likely
or serious kind of error we can expect programmers to make.

- Some ANSI-aware compilers will complain upon encountering a
function call for which no prototype has been seen.  This is a
Good Thing, to be encouraged, but is a quality of implementation
issue and can't be relied on.

- A practical consideration is the likelihood of needing to
port to pre-ANSI environments which will want casting anyway.
This alone will probably cause me to use casts with malloc()
until ANSI compilers become widespread.

- The habit of casting malloc is apparently so strong that it
survived into the 2nd edition of K&R (pp. 142-143), which says
that "the proper method is to declare that malloc returns a
pointer to void, then explicitly coerce the pointer into the
desired type with a cast."  If you indulge this habit, you are at
least in prestigious company.

(I suppose some might think this settles the question, but
appealing to the authority of the Wise Ones doesn't make the
counterarguments any less cogent.)



More information about the Comp.lang.c mailing list