Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Linked list: casting a malloc pointer

Reply
Thread Tools

Linked list: casting a malloc pointer

 
 
__frank__
Guest
Posts: n/a
 
      10-11-2005
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?


 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      10-11-2005
__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.
 
Reply With Quote
 
 
 
 
Jason Curl
Guest
Posts: n/a
 
      10-11-2005
__frank__ wrote:
> 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.

>
>

 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      10-11-2005
__frank__ wrote:
>
> 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: <(E-Mail Removed)>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How to check whether malloc has allocated memory properly in caseif malloc(0) can return valid pointer Gene C Programming 0 12-20-2010 05:33 AM
Casting of void pointer returned by malloc() SRR C Programming 41 03-11-2007 05:03 PM
Casting a file pointer to char pointer Abhishek C Programming 9 03-22-2006 07:48 AM
malloc and pointer casting Mirko C Programming 14 09-14-2005 02:14 AM
ISO C++ forbids casting between pointer-to-function and pointer-to-object ken C++ 3 11-08-2003 12:08 PM



Advertisments