Linked list: casting a malloc pointer

F

__frank__

I'm referring to linked list.

In some codes I find:

/* 1 */
newNode=malloc(sizeof (struct* node));

in other ones, instead I find:

/* 2 */
newNode (list_type) malloc (sizeof (struct* node));

Which one is correct?
 
E

Emmanuel Delahaye

__frank__ a écrit :
I'm referring to linked list.

In some codes I find:

/* 1 */
newNode=malloc(sizeof (struct* node));

in other ones, instead I find:

/* 2 */
newNode (list_type) malloc (sizeof (struct* node));

Which one is correct?
None of them (for the same reason : WtH is 'struct* node' ?)

Try this one :

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

and be sure to include <stdlib.h>

Also, test the value returned by malloc(), and free() when not used anymore.
 
J

Jason Curl

__frank__ said:
I'm referring to linked list.

In some codes I find:

/* 1 */
newNode=malloc(sizeof (struct* node));

This function allocates only enough space for the pointer of the node.
Is this what you want, or do you want to allocate enough space for the
entire node?
in other ones, instead I find:

/* 2 */
newNode (list_type) malloc (sizeof (struct* node));

This is not grammatically correct. It would be much better if you could
actually copy and paste the relevant sections of code. Otherwise we're
just guessing.

As mentioned previously in this newsgroup (read back up to a couple of
weeks), you will find many mentions about your question. Don't typecast
the result of malloc(). See the C newsgroup faq and previous posts about
why.
Which one is correct?

From what we have, the first one is better. But with no code posted,
both could be wrong.

It would be most helpful if you could post a minimal sample in C that
can be compiled (that you have also personally compiled) that
demonstrates what you are asking for.
 
K

Kenneth Brody

__frank__ said:
I'm referring to linked list.

In some codes I find:

/* 1 */
newNode=malloc(sizeof (struct* node));

in other ones, instead I find:

/* 2 */
newNode (list_type) malloc (sizeof (struct* node));

Which one is correct?

If you have included the proper header files, then both of the above
(once you fix the missing '=' for #2) are equivalent.

However...

You probably mean "sizeof(struct node)", and "sizeof(*newNode)" is even
better, as newNode's type can change and the malloc will automatically
get the right size.

And, if you don't remember to include the proper header file, then #2
will mask the error, so most people here will tell you the #1 is "better".

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top