Thread: Struct and Malloc Help

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    5

    Question Struct and Malloc Help

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct nodePayload{
     int number;
    };
    
    struct node{
     struct nodePayload *payload;
     struct node *next;
    } *headSingle, *tailSingle, *temp;
    
    S_ADD(int z){
      temp = malloc(sizeof(struct node)); //Crashes here
    
      temp->payload->number = z;
      
      if (headSingle == NULL){
       headSingle = temp;
       tailSingle = temp;
      }
      else{
        headSingle->next = temp;
        tailSingle = temp;
      }
    }
    
    main(int argc, char *argv[]){
     headSingle = NULL;
     tailSingle = NULL;
     
     int i;
     
     for (i=0; i<10; i++){
      S_ADD(1);
     }
    
    }
    Its crashing at the malloc line. I'm guessing the sizeof(struct node) part is wrong because its not taking into account the the size of nodePayload? How should it be written?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is probably crashing on the next line because temp->payload does not point to anything.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    Quote Originally Posted by laserlight View Post
    It is probably crashing on the next line because temp->payload does not point to anything.
    What should I have done?

    Also it crashes on its third running of S_ADD.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Get payload to point to something, say by using malloc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    Quote Originally Posted by laserlight View Post
    Get payload to point to something, say by using malloc.
    temp->payload = malloc(sizeof(struct nodePayload));
    I'm very new to c. I tried adding this before the first malloc, but I don't know what I'm doing. Can you spell it out for me?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code? By the way, you forgot about the return type for S_ADD. Also, rename S_ADD as fully capitalised names are conventionally reserved to macros, or at least to constants.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct nodePayload{
     int number;
    };
    
    struct node{
     struct nodePayload *payload;
     struct node *next;
    } *headSingle, *tailSingle, *temp;
    
    s_display_inorder(){
      tailSingle->next = NULL;
      temp = headSingle;
      printf("\nSingle list:\n");
      while (temp != NULL){
       printf("Left to right output:\t%d\n", temp->payload->number);
       temp = temp->next;
      }
    }
    
    s_add(int z){
    
      temp->payload = malloc(sizeof(struct nodePayload));
      temp = malloc(sizeof(struct node)); //Crashes here
      
      temp->payload->number = z;
      
      if (headSingle == NULL){
       headSingle = temp;
       tailSingle = temp;
      }
      else{
        headSingle->next = temp;
        tailSingle = temp;
      }
      return;
    }
    
    main(int argc, char *argv[]){
     headSingle = NULL;
     tailSingle = NULL;
     
     int i;
     
     for (i=0; i<10; i++){
      s_add(1);
     }
     
      s_display_inorder();
    
    }
    I just want it to put the nodes in a linked list.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, you need to assign to temp before you assign to temp->payload.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    Quote Originally Posted by laserlight View Post
    Ah, you need to assign to temp before you assign to temp->payload.
    Ah thank you very much; it's working perfectly now.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good to hear, but there's still work to be done, e.g., you need to free what you malloc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc the struct
    By farukyaz in forum C Programming
    Replies: 4
    Last Post: 10-17-2011, 04:24 AM
  2. char** in struct amd malloc()
    By alexopth1512 in forum C Programming
    Replies: 7
    Last Post: 11-03-2010, 03:05 AM
  3. another malloc of ptr in struct
    By tikelele in forum C Programming
    Replies: 7
    Last Post: 11-20-2007, 03:17 PM
  4. malloc pointers within a struct
    By tikelele in forum C Programming
    Replies: 3
    Last Post: 11-20-2007, 11:02 AM
  5. how to use malloc with a struct?
    By allplay in forum C Programming
    Replies: 6
    Last Post: 09-13-2006, 02:49 PM