Thread: How to allocate dynamic memory for structure in my code

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    81

    How to allocate dynamic memory for structure in my code

    How to allocate dynamic memory for structure in my code

    I know how to use malloc to allocate dynamic memory

    Code:
    int *ptr = (int *) malloc(4 * sizeof (int));
    if (ptr == NULL) {
        /* Memory could not be allocated
    } else {
        /* Allocation succeeded.  Do something.  */
        free(ptr);  
        ptr = NULL; /* The pointed-to-data  must not be used again,
                       unless re-assigned by using malloc
                       again. */
    }
    How to allocate dynamic memory for the structure in code ?

    Code:
    #include <stdio.h>struct Person
    
     { int N; float height; }p1;
    
    void show_person(struct Person p) {
      printf( "person %d is %f meters tall.\n", p.N, p.height );
    }
    
    void change_height(struct Person *p, float new_height) {
      p->height = new_height;
    }
    
    int main(void) {
        
        p1.N = 1;
        p1.height = 2.0;
        
       show_person(p1);
       change_height(&p1, 1.9)  ;
       show_person(p1);
    
      return 0;
    }
    Last edited by gajya; 11-08-2019 at 07:55 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you want to dynamically allocate memory?
    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
    Oct 2019
    Posts
    81
    Quote Originally Posted by laserlight View Post
    Why do you want to dynamically allocate memory?
    I have studied "dynamically allocate memory" topic. I found we mostly allocate dynamic memory for pointer and structure. I can allocate dynamic memory for pointer . I want to allocate dynamic memory for structure I am struggling to allocate dynamic memory for structure in my program

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gajya
    I have studied "dynamically allocate memory" topic. I found we mostly allocate dynamic memory for pointer and structure.
    Actually, we always dynamically allocate memory for objects, where an object is some region of memory storing a value of some type. So, an object could be of pointer type, of some struct type, of some integer type, etc.

    All that you need to do therefore is to take your example code for allocating 4 int objects and just change int to struct Person and you're done (for 4 struct Person objects; of course you have to get rid of the multiplication if you want only one).

    That said, note that you don't need to cast the return value of malloc, and except in the case where you're storing the return value in a pointer to void, you can write:
    Code:
    T *p = malloc(sizeof(*p));
    So if you do change the type T, you only need to do so in one place in the statement.
    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
    Oct 2019
    Posts
    81
    Quote Originally Posted by laserlight View Post
    That said, note that you don't need to cast the return value of malloc, and except in the case where you're storing the return value in a pointer to void, you can write:
    the statement.
    Code:
    #include <stdio.h>
    #include<stdlib.h>
    
    struct person
     
     { int N; float height; }p1;
     
    int main(void)
    {
       p1.N = 1;
       p1.height = 2.0;
       
       struct person *p;
       
       p = (struct person*) malloc(4 * sizeof(struct person));
       
       if (p == NULL)
        {
            printf("Memory not Available \n");
            exit(1);
        }
      
        else
       {
           printf("Memory allocated\n");
           printf("%d\n",  (p)->N);
       
       }
       free(p);
    
    
       return 0;
    
    
    }
    Memory allocated
    8070808
    That's what I do not expect
    Last edited by gajya; 11-09-2019 at 02:23 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Err... N is just a member of the struct. It doesn't tell you how much memory has been allocated. Furthermore, you printed its value before assigning it an initial value, so you got garbage.
    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
    Oct 2019
    Posts
    81
    Quote Originally Posted by laserlight View Post
    Err... N is just a member of the struct. It doesn't tell you how much memory has been allocated. Furthermore, you printed its value before assigning it an initial value, so you got garbage.
    tell me if missing something

    Code:
    #include <stdio.h>#include<stdlib.h>
     
    struct person
      
     { int N; float height; };
     
     void display(struct person *st)
    {
      printf("N : %d\n", st -> N);
      printf("height: %f \n", st -> height);
    }
     
    int main(void)
    {
    
    
       struct person p1;
      
        struct person *st = (struct person*) malloc(4 * sizeof(struct person));
      
         if (st == NULL)
        {
            printf("Memory not Available \n");
            exit(1);
        }
      
      p1.N = 10;
      p1.height = 5.2;
      
      display(&p1); 
    
    
       free(st);
     
       return 0;
     
    }
    N : 10
    height: 5.200000
    Last edited by gajya; 11-09-2019 at 03:33 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want to use st, why are you still declaring and using p1?
    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
    Oct 2019
    Posts
    81
    Quote Originally Posted by laserlight View Post
    If you want to use st, why are you still declaring and using p1?
    Thank you for advice

    Code:
    #include <stdio.h>#include<stdlib.h>
      
    struct person
       
     { int N; float height; };
      
     void display(struct person *st)
    {
      printf("N : %d\n", st -> N);
      printf("height: %f \n", st -> height);
    }
      
    int main(void)
    { 
        struct person *st = (struct person*) malloc(4 * sizeof(struct person));
       
         if (st == NULL)
        {
            printf("Memory not Available \n");
            exit(1);
        }
       
      st->N = 10;
      st->height = 5.2;
       
      display(st); 
     
     
       free(st);
      
       return 0;
      
    }

  10. #10
    Registered User
    Join Date
    Oct 2019
    Posts
    81
    Quote Originally Posted by laserlight View Post
    If you want to use st, why are you still declaring and using p1?
    I am going to make single linked list

    Code:
    #include <stdio.h>#include <stdlib.h>
     
    struct Person{
           int N;
           struct Person *nextPerson;
           };
     
    struct Person *addPerson(struct Person *head, int x){
           struct Person *st;
           if (head==NULL){
     
                           st = malloc(sizeof(struct Person));
                           st-> N = x;
                           st->nextPerson = NULL; 
                           }           
           return st;
            
           }
     
    void show(struct Person *head){
         
    	 struct Person *currentPerson;
         
    	 currentPerson = head;
    	 
         while (currentPerson!=NULL){
               printf("%d\n",currentPerson->N);
               currentPerson = currentPerson->nextPerson;
               }
     
         }
          
    int main(void){
         
        struct Person *head = NULL;
     
     return 0;
      
    }
    How to set each person to store N data in list ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-03-2019, 11:46 AM
  2. structure dynamic memory allocation?
    By mgracecar in forum C Programming
    Replies: 4
    Last Post: 03-15-2012, 11:15 PM
  3. Freeing dynamic allocated memory of structure
    By darekg11 in forum C Programming
    Replies: 14
    Last Post: 01-10-2011, 09:17 AM
  4. Why does this structure allocate memory?
    By sanddune008 in forum C Programming
    Replies: 18
    Last Post: 07-14-2009, 07:31 AM
  5. Allocate a big dynamic memory location
    By vnrabbit in forum C Programming
    Replies: 11
    Last Post: 10-09-2002, 08:02 AM

Tags for this Thread