Linked list: casting a malloc pointer

Discussion in 'C Programming' started by __frank__, Oct 11, 2005.

  1. __frank__

    __frank__ Guest

    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?
     
    __frank__, Oct 11, 2005
    #1
    1. Advertising

  2. __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.
     
    Emmanuel Delahaye, Oct 11, 2005
    #2
    1. Advertising

  3. __frank__

    Jason Curl Guest

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

    >
    >
     
    Jason Curl, Oct 11, 2005
    #3
  4. __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: <mailto:>
     
    Kenneth Brody, Oct 11, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kurt

    malloc() and free() and linked list

    Kurt, Dec 16, 2004, in forum: C Programming
    Replies:
    1
    Views:
    448
    Lawrence Kirby
    Dec 16, 2004
  2. Mirko

    malloc and pointer casting

    Mirko, Aug 31, 2005, in forum: C Programming
    Replies:
    14
    Views:
    604
    Dave Thompson
    Sep 14, 2005
  3. fool
    Replies:
    14
    Views:
    544
    Barry Schwarz
    Jul 3, 2006
  4. joshd
    Replies:
    12
    Views:
    702
    John Carson
    Oct 2, 2006
  5. SRR
    Replies:
    41
    Views:
    1,327
    Yevgen Muntyan
    Mar 11, 2007
Loading...

Share This Page