You don't need to cast malloc, because it returns a void*.
As wiki says: "The use of casting is required in C++ due to the strong type system, whereas this is not the case in C."
So, if you're working with C, you don't need to cast the malloc. But there are some advantages and disadvantages to do so:
Advantages:
- Including the cast allows a program or function to compile as C++.
- The cast allows for pre-1989 versions of malloc that originally returned a char *.
- Casting can help the developer identify inconsistencies in type sizing should the destination pointer type change, particularly if the pointer is declared far from the malloc() call.
Disadvantages:
- Under the ANSI C standard, the cast is redundant.
- If the type of the pointer is changed, one must fix all code lines where malloc was called and cast (unless it was cast to a typedef).