Specifically, what's dangerous about casting the result of malloc?

Viewed 18697

Now before people start marking this a dup, I've read all the following, none of which provide the answer I'm looking for:

  1. C FAQ: What's wrong with casting malloc's return value?
  2. SO: Should I explicitly cast malloc()’s return value?
  3. SO: Needless pointer-casts in C
  4. SO: Do I cast the result of malloc?

Both the C FAQ and many answers to the above questions cite a mysterious error that casting malloc's return value can hide; however, none of them give a specific example of such an error in practice. Now pay attention that I said error, not warning.

Now given the following code:

#include <string.h>
#include <stdio.h>
// #include <stdlib.h>

int main(int argc, char** argv) {

    char * p = /*(char*)*/malloc(10);
    strcpy(p, "hello");
    printf("%s\n", p);

    return 0;
}

Compiling the above code with gcc 4.2, with and without the cast gives the same warnings, and the program executes properly and provides the same results in both cases.

anon@anon:~/$ gcc -Wextra nostdlib_malloc.c -o nostdlib_malloc
nostdlib_malloc.c: In function ‘main’:
nostdlib_malloc.c:7: warning: incompatible implicit declaration of built-in function ‘malloc’
anon@anon:~/$ ./nostdlib_malloc 
hello

So can anyone give a specific code example of a compile or runtime error that could occur because of casting malloc's return value, or is this just an urban legend?

Edit I've come across two well written arguments regarding this issue:

  1. In Favor of Casting: CERT Advisory: Immediately cast the result of a memory allocation function call into a pointer to the allocated type
  2. Against Casting (404 error as of 2012-02-14: use the Internet Archive Wayback Machine copy from 2010-01-27.{2016-03-18:"Page cannot be crawled or displayed due to robots.txt."})
7 Answers

The malloc() function could often require a conversion cast before. For the returned type from malloc it is a pointer to void and not a particular type, like may be a char* array, or a string. And sometimes the compiler could not know, how to convert this type.

int size = 10; 
char* pWord = (char*)malloc(size); 

The allocation functions are available for all C packages. So, these are general functions, that must work for more C types. And the C++ libraries are extensions of the older C libraries. Therefore the malloc function returns a generic void* pointer.

Cannot allocate an object of a type with another of different type. Unless the objects are not classes derived from a common root class. And not always this is possible, there are different exceptions. Therefore a conversion cast might be necessary in this case.

Maybe the modern compilers know how to convert different types. So this could not be a great issue, when is doing this conversion. But a correct cast can be used, if a type conversion is possible. As an example: it cannot be cast "apples" to "strawberries". But these both, so called "classes", can be converted to "fruits".

There are custom structure types, which cannot be cast directly. In this case, any member variable has to be assigned separately. Or a custom object would have to set its members independently. Either if it is about a custom object class, or whatever else...

Also a cast to a void pointer must be used when using a free call. This is because the argument of the free function is a void pointer.

free((void*)pWord); 

Casts are not bad, they just don't work for all the variable types. A conversion cast is also an operator function, that must be defined. If this operator is not defined for a certain type, it may not work. But not all the errors are because of this conversion cast operator.

With kind regards, Adrian Brinas

Related