Malloc syntax in C

21,891

Solution 1

The code is dynamically creating a pointer to a single type of struct node. In most versions of C, the (struct node *) cast is not needed, and some argue that it shouldn't be used. If you remove the cast, it will be a void*, which can be used for any type.

Therefore:

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

is roughly equivalent to:

newnode = malloc(sizeof(struct node));

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

Note 1: If you are using Visual Studio to write your C code, it will give you red underlining if you don't cast the result of malloc. However, the code will still compile.

Note 2: Using malloc in C++ code requires you to cast the result as shown in your example.

Solution 2

You ran into a very bad code. C programmers never cast a result of malloc(). Not only it is not needed but can be harmful.

Solution 3

You should pass the number of bytes that you want malloc to allocate as argument. That is why in this code sample you use sizeof(struct node) telling C to allocate the number of bytes needed for a struct node variable. Casting the result like is shown in this code is bad idea by the way.

Solution 4

malloc returns a void pointer .

(struct node*) does a explicit conversion from void pointer to the target pointer type

Solution 5

"malloc" returns a void-pointer. (struct node *) is type-casting the result of malloc to a "node struct pointer", which is (undoubtibly) what "newnode" is.

Share:
21,891
Shy Student
Author by

Shy Student

Updated on February 11, 2020

Comments

  • Shy Student
    Shy Student about 4 years

    In the books I read that the syntax for malloc is malloc(sizeof(int)) but in one of doubly linked list program I see the following:

    newnode=(struct node *)malloc(sizeof(struct node))
    

    What is (struct node*) doing here? What is this entire code doing? btw, the code for the struct in the program is as below.

    struct node
    {
    char line[80];
    struct node *next,*prev;
    };
    
    struct node *start=NULL,*temp,*temp1,*temp2,*newnode;
    

    Thank you

  • teppic
    teppic about 11 years
    The consensus on here seems not to do it, but I think it's important to mention that a lot of books recommend it - including 'The C Programming Language' itself.
  • Inisheer
    Inisheer about 11 years
    @teppic Correct. There has even been much debate about the topic here on several StackOverflow questions. Personally, I think the hate for casting the return of malloc is a bit overblown.
  • Shy Student
    Shy Student about 11 years
    Thank you @Inisheer.. that was exactly what I wanted to know!! :)
  • Argeman
    Argeman about 11 years
    This is highly discussable, as you can run into similar problems the other way round, look here: securecoding.cert.org/confluence/display/seccode/…
  • Wiz
    Wiz about 11 years
    @Inisheer It's not overblown. If you didn't include the correct header, you'd be casting what the compiler assumes a function that returns int to struct node*. Effectively hiding the error. There should be no cast on that line. Secondly, the question is tagged with C, what C++ does has no bearing here. Thirdly, a bug in a compiler (or an IDE) is no reason to use bad code.
  • Inisheer
    Inisheer about 11 years
    @Wiz You are correct. But I think the overall level of distaste for casting is a bit much. But that's my opinion. Secondly, although the question was tagged C, I assumed from the content of the question that the OP was a novice C programmer and could benefit from the notes about C++ in the event he/she uses Visual Studio or sees malloc being cast within C++ code in the future. Effectively, I'm trying to directly answer the question, but also provide possible guidance in the future.
  • Inisheer
    Inisheer about 11 years
    Bottom line: there are much MUCH worse "errors" you can make in C and C++ than casting the return of malloc.