Thread: Puzzled

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    147

    Puzzled

    struct LLNode *initLLNode(char val){
    struct LLNode *temp;
    temp=(struct LLNode *)malloc(sizeof(struct LLNode));
    temp->next=NULL;
    temp->key=val;
    return(temp);
    }

    i have these lines of code...the line which puzzles me is the line with (struct LLNode *) next to malloc
    wat is its purpose there?
    Only by the cross are you saved...

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    malloc returns a void pointer (pointer of no particular type but one that can be cast to become of any type) so if you casted it as struct LLNode *, then malloc will return that type.

    correct me if im wrong
    there are only 10 people in the world, those who know binary and those who dont

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >correct me if im wrong
    You're wrong. C allows implicit conversion from a void* to any pointer type (except function pointers). The cast is unnecessary and dangerous as it potentially hides the error of not including stdlib.h. The preferred way to call malloc in the C community is as follows:
    Code:
    temp = malloc(sizeof (*temp));
    
    if (temp)
    {
        /* Use the pointer safely now */
    }
    else
    {
        /* Recover from a failed call to malloc */
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    i see, i don't get the part when u said it po tentially hides the error of not including stdlib.h, where does this stdlib.h suddenly get involved in here?
    Only by the cross are you saved...

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >where does this stdlib.h suddenly get involved in here?
    malloc is declared in stdlib.h. If you fail to include the standard header in which a function is declared, any use of that function is undefined. In short, your program is very wrong and will fail to link if you're lucky. If you're not lucky then it will run, but with unpredictable behavior.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    so the problem with using this kind of malloc assignment is dangerous?

    but i still don't see how dangerous it can be by using this way, probably u could help me by elaborating further?
    Only by the cross are you saved...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Puzzled.
    By silhoutte75 in forum C Programming
    Replies: 13
    Last Post: 01-21-2008, 05:17 PM
  2. Replies: 4
    Last Post: 07-15-2005, 04:10 PM
  3. Puzzled
    By sketchit in forum C Programming
    Replies: 3
    Last Post: 10-30-2001, 08:11 PM
  4. Still puzzled [I hope this formatted]
    By sketchit in forum C Programming
    Replies: 2
    Last Post: 09-28-2001, 12:26 AM
  5. Still puzzled
    By sketchit in forum C Programming
    Replies: 1
    Last Post: 09-27-2001, 05:09 PM