Is it Bad to Cast malloc()?

In my code, in my books, and in my online courses, I typecast the malloc() function. This is something I’ve done for a while, but never really knew why — until now. I’ve also learned that doing so is considered “bad programming practice” by some in the C community.

The malloc() function allocates a chunk of memory. The function returns the memory chunk’s address as a void pointer: void *

Somewhere along the line, I started typecasting the malloc() function to match the data type of its pointer assignment. So if blorf is a char pointer, I’d do this:

blorf = (char *)malloc( 1024 );

This statement allocates a 1024 byte buffer and assigns its address to char pointer blorf. The (char *) typecast transforms the void pointer into a char pointer for the assignment. Yet, this typecast is unnecessary in the C language.

Somewhere at some time, I probably read that typecasting the return value of malloc() was strongly suggested. I forget the specifics, other than I started typecasting malloc() out of habit. Then I learned that typecasting malloc() is “bad programming practice,” though even this assertion is debated.

First, in the C++ language, typecasting malloc() is mandatory. Due to the crossover of C and C++ material, this is probably the reason why I started typecasting malloc(): I read some C code somewhere written by a C++ programmer who just typecasts malloc() for reasons in one language that aren’t required in the other.

Second, the thing that offends some C programmers the most is that by typecasting malloc() you run the risk of improperly identifying the function. In this sense, typecasting works like a mask: When any function is typecast, the compiler may not bother looking up its definition by checking a prototype. I’m unsure of the specifics, but even on this point I see a lot of back-and-forth between C experts.

In the following code, I typecast malloc() without including the stdlib.h header file, which is where the function’s prototype exists:


#include <stdio.h>

int main()
    int *p;

    p = (int *)malloc( 1024 );
    if( p==NULL )
        printf("malloc() failed\n");
        printf("malloc() succeeded\n");


The program doesn’t build when using clang or gcc: Error messages are output, specifically stating that malloc() is a library function and it requires the stdlib.h header file. Good for these compilers!

With the MinGW compiler, however, the program builds. Warnings are generated, just like with clang and gcc: The malloc() function requires the stdlib.h header file — but the program builds anyway. And it runs. This result is why some programmers caution against typecasting the malloc() function.

Though it isn’t what I consider a Great Sin, I shan’t be typecasting malloc() in my code from here on. But I also feel it’s an overreaction to say that typecasting malloc() is “bad” programming practice. Given the errors and warnings generated by modern compilers, I’d say typecasting malloc() isn’t bad, just unnecessary and easily avoidable.

Leave a Reply