Like Tree1Likes
  • 1 Post By laserlight

Struct and Malloc Help

This is a discussion on Struct and Malloc Help within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> struct nodePayload{ int number; }; struct node{ struct nodePayload *payload; struct node *next; } *headSingle, ...

  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
    21,732
    It is probably crashing on the next line because temp->payload does not point to anything.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,732
    Get payload to point to something, say by using malloc.
    Tclausex likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,732
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,732
    Ah, you need to assign to temp before you assign to temp->payload.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,732
    Good to hear, but there's still work to be done, e.g., you need to free what you malloc.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02:17 PM
  4. malloc pointers within a struct
    By tikelele in forum C Programming
    Replies: 3
    Last Post: 11-20-2007, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21