In general, the return value of
malloc() should be used directly in an assignment or initialization. I don't recommend casting its return value:
The cast is not required in ANSI C.
Casting its return value can mask a failure to
#include <stdlib.h>, which leads to undefined behavior. As C99 slows becomes more popular, this will become less of an issue, because C99 requires all functions to be declared before they are called.
If you cast to the wrong type by accident, odd failures can result. This is especially true if
#included, as above, but alignment can still cause trouble on particularly odd systems.
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$nK2.email@example.com.