Why are there multiple religions in the world? Why did the US invade
Iraq? Why are there people who think 9/11 was a conspiracy? Why do
people think its a good idea to omit the cast for malloc? Whatever
the answer is to these questions, I am sure that they are all highly
related.
I don't recommend casting the return value of malloc():
* The cast is not required in ANSI C.
But it is required by C++. Thus you are just making your code
incompatible with C++. C++ has stronger type checking in general, and
thus its a good idea to compile your C code with a C++ compiler, even
if just for checking purposes -- you can't do that if you omit these
casts.
* Casting its return value can mask a failure to #include
<stdlib.h>, which leads to undefined behavior.
I am not aware of any modern compiler for which this error isn't
already pointed out to you whether you cast or not.
* If you cast to the wrong type by accident, odd failures can
result.
If you cast to the wrong type by accident, your compiler will tell you
-- that's the point. Actually leaving off the cast means you give up
any opportunity to type check when you change variables or type
definitions. There are "find and replace" scenarios that may too
easily fail if you omit this cast.
Basically, you don't *NEED* to do the cast, but its a question of
being able to write code in a scalable and maintainable way. Putting
the cast there reduces the probability of errors in your code, and
makes it far easier to maintain.