Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

If stdlib.h is not included, the compiler makes the assumption that malloc() has a return type of int. When the result of a call to malloc() is explicitly cast to a pointer type, the compiler assumes that the cast from int to a pointer type is done with full knowledge of the possible outcomes. This may lead to behavior which that is unexpected by the programmer.

...

By ommiting the explicit cast to a pointer, the compiler can determine that an int is attempting to be assigned to a pointer type and will generate a warning which that may easily be corrected.

Code Block
#include <stdlib.h>
...
char *p = malloc(10);

...

References

comp.lang.c FAQ list - Question 7.7 http://c-faq.com/malloc/cast.html
comp.lang.c FAQ list - Question 7.7b http://c-faq.com/malloc/mallocnocast.html