-2

Currently learning C and learning about linked lists.

While looking through the examples I see this bit of code:

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

My question is why is malloc being multiplied with a Node pointer? From my understanding to allocate memory you just need to use malloc and give it the parameter of a Node.

Also why is the parameter inside malloc just a struct Node instead of being a struct Node* when the variable is a pointer?

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • 2
    Side note: In contrast to C++, in C, casting the result of `malloc` is not necessary. You may want to read this for further information: [Do I cast the result of malloc?](https://stackoverflow.com/q/605845/12149471) – Andreas Wenzel Apr 02 '23 at 03:04
  • Using a cast (although strictly unnecessary and preferably avoided), is often done for compatibility with C++ (especially with header files that define inlined or static functions). – Myst Apr 02 '23 at 04:07
  • 3
    `malloc` isn't being multiplied with anything. In this context, the `*` means "pointer". That is, it's casting the return value from `malloc` from a void pointer (a `void*` type) to a `struct Node` pointer (a `struct Node*` type). Also note that casting the return value of `malloc` and friends is [generally discouraged](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – yano Apr 02 '23 at 04:12
  • as for your 2nd question, you want to allocate memory for the _thing being pointed to_, which is "one level up" from the pointer. You have a `struct Node*`. This must point to a `struct Node`, so you allocate enough space for one (or more) `struct Node`(s). The preferred way is `struct Node* node = malloc(sizeof(*node));`. WIth that, the type of `node` can change to anything (eg, `struct Node2* node`) and the argument to `malloc` doesn't need any updating, since `*node` transforms into the correct type "automatically". – yano Apr 02 '23 at 04:29
  • Does this answer your question? [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Andreas is moving to Codidact Apr 02 '23 at 15:21
  • 1
    @Andreasdet: Even if your proposed dupicate question is probably useful for OP, it does not answer OP's main question. – Andreas Wenzel Apr 02 '23 at 15:45
  • @AndreasWenzel Would a question about C syntax be a potentially better duplicate target? – Andreas is moving to Codidact Apr 02 '23 at 15:47
  • @Andreasdet: I don't think that it is appropriate to close this question at all (also not as a duplicate), unless you find a question that is very similar to this one (which is probably unlikely). – Andreas Wenzel Apr 02 '23 at 15:50

1 Answers1

2

As stated in the documentation of malloc, that function requires one parameter which specifies the number of bytes to allocate.

Therefore, in order to allocate sufficient memory to represent a single node, you must pass the size of an object of type struct Node to the function malloc.

Although it may be possible for you to calculate this size manually, the easiest way to determine the size of an object of type struct Node is to use the sizeof operator. That is why the expression sizeof(struct Node) is being passed as a parameter to the function malloc.

Using the expression sizeof(struct Node*) instead of sizeof(struct Node) as a parameter for malloc would be wrong, as that would only allocate sufficient memory for a pointer, but you want to allocate sufficient memory for an object of type struct Node (which is probably larger than a pointer).

In the line

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

the (struct Node*) is a type cast. It converts the data type void * returned by malloc to a struct Node *.

In this context, the * means "pointer", not multiplication. If you are unfamiliar with the concept of pointers, then I suggest that you first learn the basics of the programming language C, before you attempt to understand this. For example, here is a list of books. I suggest that you buy one under the category "Beginner".

Note that in contrast to C++, in C, casting the result of malloc is not necessary and generally discouraged. See the following question for further information:

Do I cast the result of malloc?

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39