55

I wanted to ask about the following case:

char *temp;
temp = malloc(10);

Since the return type of malloc is void*, will the pointer returned by the malloc be implicitly cast to char* type before being assigned to temp? What does the standard say in this regard?

If our pointer variable is some struct type for example:

struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));

If we allocate memory to temp without casting it to struct node* type, will it be implicitly cast to struct node* type or is it necessary to explicitly cast it to struct node* type?

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
mawia
  • 9,169
  • 14
  • 48
  • 57
  • 8
    If you might ever need to compile the code with a C++ compiler instead of a C compiler, then the cast is required. Consequently, most of my code does include the explicit cast - even though pure C does not require it. I usually tag it with /*=C++=*/ to indicate why. – Jonathan Leffler Jun 04 '09 at 23:32
  • Though not necessary, for me helps me to read code later with little verbosity. – Xolve Aug 07 '10 at 06:26
  • 2
    See also [this question](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). – unwind Jun 04 '13 at 14:50

4 Answers4

53

If you like the "don't repeat yourself" mindset, it should be appealing that you don't need to repeat the type name from the declaration of the variable, in the malloc() call. Because, as folks have pointed out, you don't: pointers convert to and from void * without loss, with the exception of function pointers.

Also, on that note, you don't need to repeat yourself with the use of sizeof either. Your second example, when allocating a structure, can be written like this:

struct node *temp;
temp = malloc(sizeof *temp);

Which in my not so humble opinion is the best way.

Avoiding repeating yourself cuts down on the number of things you write, which in turn cuts down on the risk that any of those things are wrong.

Note the asterisk in the sizeof argument, this means "the size of the object pointed to by this pointer", which is of course the same as "the size of the type struct node" but without repeating the type name. This is because sizeof computes (at compile-time!) the size of the expression that is its argument. For this case. Just like sizeof 3 computes the size of an expression of type int, sizeof *temp computes the size of an instance of struct node.

Sure, you do repeat something, i.e. the variable name itself, but that is often a simpler expression and easier to get right, and also can be easier for the compiler to spot an error in.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 4
    This is one instance where applying DRY is an unsafe programming practice (see link that follows). The argument that avoiding repetition reduces errors is contextual. When it comes to pointers in C, so infamous for ambiguity, it will lead to bad juju. I rather be verbose and repeat the casts to find problems at compile time (as opposed to run-time): https://www.securecoding.cert.org/confluence/display/seccode/MEM02-C.+Immediately+cast+the+result+of+a+memory+allocation+function+call+into+a+pointer+to+the+allocated+type – luis.espinal Oct 22 '12 at 15:44
  • 3
    As usual, securecoding.cert.org is full of crap. Or, put more politely, every single article I've read on that site is full of incorrect information (think of the `ftell` vs `fstat` one), style that's widely agreed to be bad and harmful, and/or just plain cargo-cult "security" programming. – R.. GitHub STOP HELPING ICE Jan 08 '13 at 01:26
  • 2
    @luis.espinal Huh? The casts can *hide* errors, since they explicitly say to the compiler "do this!" and typically prevents it from generating warnings even if what you're asking for seems wrong. Any time you see a cast C code you need to think *harder* if what is going on is required, and why the cast was deemed necessary in the first place. Never cast if you don't have to. – unwind Sep 04 '13 at 10:19
  • 1
    I personally don't like the sizeof(*temp) notation. It's a little involved how sizeof works when expressions are used as arguments (heck, you can even used undefined functions and get no linker errors). Here it might be confusing, cause usually the * operator is bad on dangling or null pointers. Here it magically works. I think it's just clearer (especially to non experienced programmer that will stumble on your code) to just write sizeof(struct node). Only my personal opinion. – bartgol Jul 24 '14 at 18:24
34

A void pointer in C can be assigned to any pointer without an explicit cast.

MSN
  • 53,214
  • 7
  • 75
  • 105
tekBlues
  • 5,745
  • 1
  • 29
  • 32
11

C implicitly casts from and to void*, so the cast will be done automatically. In C++ only conversion to void* would be done implicitly, for the other direction an explicit cast is required.

sth
  • 222,467
  • 53
  • 283
  • 367
4

In C++ you must explicitly cast, but this is really just the language telling you off for doing it.
In c there isn't really any need to cast, memory is just memory - I will have to do a search to see if the latest C standard requires it.

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263