3

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

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
@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.
Thank you @Inisheer.. that was exactly what I wanted to know!! :)
@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.
@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.
|
3

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.

1 Comment

This is highly discussable, as you can run into similar problems the other way round, look here: securecoding.cert.org/confluence/display/seccode/…
2

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.

Comments

2

malloc returns a void pointer .

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

Comments

1

"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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.