Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I explicitly cast malloc()'s return value? [duplicate]

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?

like image 461
mawia Avatar asked Jun 04 '09 21:06

mawia


People also ask

Should I cast the result of malloc?

In C, you don't need to cast the return value of malloc . The pointer to void returned by malloc is automagically converted to the correct type. However, if you want your code to compile with a C++ compiler, a cast is needed.

What is the default return type of malloc )? Why do we need to typecast it?

As the return type of malloc is void*, when you assign the result to a pointer, it is converted implicitly to the new type. So, there is no need for explicit casting.

What is the return value of malloc () function?

The malloc() function returns a pointer to the reserved space. The storage space to which the return value points is suitably aligned for storage of any type of object. The return value is NULL if not enough storage is available, or if size was specified as zero.

Why we use typecasting in malloc?

In C++ language, by default malloc() returns int value. So, the pointers are converted to object pointers using explicit casting.


1 Answers

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.

like image 115
unwind Avatar answered Sep 21 '22 22:09

unwind