If typecasting the return of malloc()is considered to be a bad practice, why Turbo C forces us to it
about typecasting malloc (), is bad practice, why ?
Collapse
X
-
Visual Studio.NET also forces you to cast the return of
malloc().
malloc() returns a void* which means no type. When you assign a pointer to "no type" to a pointer to "a real type" it gags the type safety check of the compiler. This forces you to cast so the assignment uses the same type for the RVAL and LVAL.
Supposedly, you can assign a void* to any other pointer without a cast according to the rules of C but apparently your compiler and mine disallow it.
As to the bad practice, you need to remember that casting is a bad practice as is all void* which must be cast in order to be used.
Unfortunately, C is very primitive and uses the void* to work around the absence of function overloading you see in more modern languages.
Comment