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

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Shy Student
  • 99
  • 1
  • 1
  • 7

5 Answers5

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.

Community
  • 1
  • 1
Inisheer
  • 20,376
  • 9
  • 50
  • 82
  • 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 Mar 21 '13 at 17:15
  • @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. – Inisheer Mar 21 '13 at 17:17
  • Thank you @Inisheer.. that was exactly what I wanted to know!! :) – Shy Student Mar 21 '13 at 17:17
  • @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 Mar 21 '13 at 18:16
  • @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 Mar 21 '13 at 18:20
  • Bottom line: there are much MUCH worse "errors" you can make in C and C++ than casting the return of malloc. – Inisheer Mar 21 '13 at 18:21
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.

Community
  • 1
  • 1
  • This is highly discussable, as you can run into similar problems the other way round, look here: https://www.securecoding.cert.org/confluence/display/seccode/MEM02-C.+Immediately+cast+the+result+of+a+memory+allocation+function+call+into+a+pointer+to+the+allocated+type – Argeman Mar 21 '13 at 17:19
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.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
2

malloc returns a void pointer .

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

Pradheep
  • 3,553
  • 1
  • 27
  • 35
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.

Brad
  • 11,262
  • 8
  • 55
  • 74