How should malloc()'s return value be used?

In general, the return value of malloc() should be used directly in an assignment or initialization. I don't recommend casting its return value:

Some people do disagree, often because such casts are required in C++. But good C++ code should generally avoid malloc() entirely, using C++'s new instead. Most of the time, it doesn't make sense to compile code as both C and C++, outside of specialized circumstances such as those described by P.J. Plauger in article 9sFIb.9066$

Last updated 05 Jan 2004 12:19. Copyright © 2004 Ben Pfaff.
May be freely redistributed, but copyright notice must be retained.