As we know return type of malloc() is (void *).
If you won't cast the return of malloc(), there is no problem in C because it allows void pointers to be implicitly converted to any other object pointer type.
But C++ does not.
So if you wanted to port your code to C++, it will give error.
Assume if you won't cast the malloc() and forget to include the stdlib.h where malloc() is declared.
Then by default C will assume that the function returns int. Here you will get a warning like assigning int to pointer.
Casting the result of malloc() in C will supress a useful diagnostic (i.e a valid warnig) if you forget to include stdlib.h and may face runtime issues.
Casting can help the developer identify inconsistencies in type sizing.
If the type of the pointer is changed, one must fix all code lines where malloc (if casted) was called.