Segev
Segev

Reputation: 19303

pointer to malloc?

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

I don't understand the * here : ...(struct node*) malloc(siz... First, the * belongs to node or malloc? what does it mean? how pointers got anything to do with the memory function malloc? I'm really confused with the * location

Thanks

Upvotes: 4

Views: 1859

Answers (6)

jacekmigacz
jacekmigacz

Reputation: 789

struct node     <= type
*new_node       <= pointer named new_node (of type struct node)
=               <= assignment
(struct node *) <= cast to pointer of type struct node
malloc(SIZE)    <= returns pointer of type void, thus needs cast

Upvotes: 2

Anup H
Anup H

Reputation: 549

It is just a type casting. Actually malloc function returns void pointer (void *) , meaning it is not specific to any data structure or data type. It returns a generic pointer. By type casting we make the returned value specific, in this case (struct node *) which is a pointer to node type of struct.

In simple terms we can say we are converting the void pointer (void *) into struct pointer (struct node *).

Upvotes: 0

Vijay
Vijay

Reputation: 67221

It is just type casting.

malloc returns a void pointer(pointer which does not have any type). (struct node *) means you are assining a type to the void pointer returned by malloc. also see this

Upvotes: 0

Lundin
Lundin

Reputation: 213832

(struct node*) is cast, which is an explicit type conversion. The * belongs to struct node, stating that the type requested is a pointer to a struct node variable. malloc() always returns a void*, a void pointer.

The function definition of malloc is void* malloc (size_t size); (and the * belongs to void in that case as well).

The person who wrote the code posted in your question casted this void pointer into a struct node*. Why they did this isn't clear, they were either confused about how pointer casts in C work and also unaware of the dangers with such casts, or the code was written for C++.

A void pointer in the C language is a generic pointer type that can be converted to any other pointer type, without a cast (1).

In C++ however, you must always have a cast when converting a void pointer, because C++ has stricter type conversion rules than C and enforces an explicit conversion.

Casting the result of malloc() in C is dangerous and considered a bug (2), because unless the correct function prototype is provided through #include , then the compiler default behavior kicks in and it will incorrectly believe that malloc returns int. Depending on pointer and integer widths of the particular system, it may work just fine, or it may cause the program to crash and/or leak memory.

Casting the result of malloc() in C++ is however necessary.


References:

  1. ISO 9899:2011 6.3.2.3/1.
  2. Specifically, what's dangerous about casting the result of malloc?

Upvotes: 1

unwind
unwind

Reputation: 399823

A type name in parenthesis (such as (struct node *)) in C is called a "cast". It's a way to alter the type of an expression into the named type, and is commonly used with pointers.

However, this is code that ignores the advice in Do I cast the result of malloc. The cast is not needed here, making this (in my opinion, and perhaps not very surprisingly if you've followed that link) rather bad code.

The best way to write this allocation is:

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

This mentions the type exactly once, drops the cast which can introduce errors and is both pointless and cumbersome to read, and avoids introducing brittleness when specifying how much memory to allocate. It's win, win win.

Upvotes: 6

amit
amit

Reputation: 178451

The * is for the type. The type in here is a struct node* (a pointer to a struct node).

It is not "connected" to the malloc function, it is "connected" to the type. The malloc() returns a pointer, and thus you assign the return value as a pointer as well.

Upvotes: 0

Related Questions