Thread: dynamic memory allocation

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    dynamic memory allocation

    I have allocated dynamic memory for array and pointer in following example

    dynamic memory allocation for array

    Code:
    #include<stdio.h>
    
    int main()
    {
        int size = 10 ;
    	
        int * array = malloc(size * sizeof(int));
    	
        return 0;
    }
    dynamic memory allocation for pointer

    Code:
    #include<stdio.h>
    
    int main()
    {
        int size = 20 ;
    	
        int * ponter = malloc(size * sizeof(int));
    	
        return 0;
    }
    Is it correct way to allocate dynamic memory ?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Other than the variable name there doesn't appear to be anything different between the two snippets.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by jimblumberg View Post
    Other than the variable name there doesn't appear to be anything different between the two snippets.
    how to allocate dynamic memory for this structure

    Code:
    #include <stdio.h>
    
    struct student{
        char *name;
        int id;
        int age;
    }Data;
    int main()
    {
     .............
    ............
         return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First a couple of questions.

    1. Why do you have the global variable Data?

    2. Do you know how to make a "regular" array of that structure?

    3. How are you going to allocate memory for that pointer member variable?

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by jimblumberg View Post
    First a couple of questions.

    1. Why do you have the global variable Data?
    We can declare structure as global variable Data

    2. Do you know how to make a "regular" array of that structure?
    Yes Let's take an example:

    Code:
    struct car
    {
        char maker[30];
        char model[30]; 
        int year;
    };
    3. How are you going to allocate memory for that pointer member variable?
    I wanted to learn about dynamic memory allocation for array pointer and structure

    I was looking the way so I explained example for array and pointer and in last post I am trying to understand dynamic memory allocation for structure
    Last edited by abhi143; 10-06-2018 at 12:02 PM.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    We can declare structure as global variable Data
    Yes you can, but why are you? You're not using that horrible global variable so why define it?

    Yes Let's take an example:
    So show me how you would create a normal (no dynamic memory involved) array of that structure.

    I wanted to learn about dynamic memory allocation for array pointer and structure
    Do you realize that a dynamically allocated array starts out as a pointer? Your first post had two snippets that only differed in the name of the variable, but not in the concept.


    Okay, so what did you try? And remember you're first snippet had a pointer member variable that will need memory allocated for it as well.

  7. #7
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by jimblumberg View Post
    So show me how you would create a normal (no dynamic memory involved) array of that structure.

    Okay, so what did you try? .
    Array of structure with no dynamic memory

    Code:
    #include<stdio.h>#include <string.h>
    
    
    struct student{
      int rollno;
      char name[10];
    };
    int main()
    {
       int i;
       struct student record[3];
       printf("Enter Records of 3 students");
       
       for(i=0;i<3;i++)
       {
          printf("\nEnter Rollno:"); 
          scanf("%d",&record[i].rollno);  
          printf("\nEnter Name:");   
          scanf("%s",&record[i].name);
       }
     
        printf("\nStudent Information List:");
     
        for(i=0;i<3;i++)
        {  
          printf("\nRollno:%d, Name:%s",record[i].rollno,record[i].name);
        }
    return 0;
    }
    Array of structure with dynamic memory

    Code:
    #include<stdio.h>#include <string.h>
    
    
    struct student{
      int rollno;
      char name[10];
    };
    int main()
    {
        struct student *ptr;
        int i, Records = 3;
       
       ptr = (struct student*) malloc (Records * sizeof(struct student));
       
       for(i=0; i<record; i++)
       {
          printf("\nEnter Rollno:"); 
          scanf("%d",&record[i].rollno);  
          printf("\nEnter Name:");   
          scanf("%s",&record[i].name);
       }
     
        printf("\nStudent Information List:");
     
        for(i=0; i<record; i++)
        {  
          printf("\nRollno:%d, Name:%s",record[i].rollno,record[i].name);
        }
    return 0;
    }
    Last edited by abhi143; 10-08-2018 at 10:03 AM.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Okay, good first try. But you forgot to define the variable "record", you should probably be using "Records" instead.

    You should get rid of the cast of the return value from malloc(), that cast can hide some mistakes. And by the way you're making one of those mistakes in your code. You need to #include the proper header file for the malloc() function.

    Lastly for now, you should never use a function to retrieve a C-string that doesn't limit the number of characters it will try to retrieve to help avoid possible buffer overflow errors. With scanf() you should be using the proper width specifier (9 in this program).

  9. #9
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by jimblumberg View Post
    Okay, good first try. But you forgot to define the variable "record", you should probably be using "Records" instead.

    You should get rid of the cast of the return value from malloc(), that cast can hide some mistakes. And by the way you're making one of those mistakes in your code. You need to #include the proper header file for the malloc() function.
    I did mistake while copy paste and editing code

    Lastly for now, you should never use a function to retrieve a C-string that doesn't limit the number of characters it will try to retrieve to help avoid possible buffer overflow errors. With scanf() you should be using the proper width specifier (9 in this program).
    I didn't understand this paragraph. can you give me more detail

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I didn't understand this paragraph. can you give me more detail
    What don't you understand?

    Did you fix this?
    You should get rid of the cast of the return value from malloc(), that cast can hide some mistakes. And by the way you're making one of those mistakes in your code. You need to #include the proper header file for the malloc() function.

  11. #11
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by jimblumberg View Post
    What don't you understand?

    Did you fix this?
    I have fixed it

    Code:
    #include<stdio.h>
    #include <string.h>
    #include<stdlib.h>
    
    
    struct student{
      int marks;
      char name[10];
    };
    int main()
    {
        struct student *ptr;
        int i, record = 3;
       
        ptr = (struct student*) malloc (record * sizeof(struct student));
       
     
        for(i = 0; i < record; ++i)
       {
           printf("Enter name of the student and marks respectively:\n");
           scanf("%s %d", &(ptr+i)->name, &(ptr+i)->marks);
       }
       
         printf("Displaying Information:\n");
    
    
       for(i = 0; i < record ; ++i)
           printf("%s\t%d\n", (ptr+i)->name, (ptr+i)->marks);
           
        return 0;
    }

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ptr = (struct student*) malloc (record * sizeof(struct student));
    Remove the cast, compile it, and tell us what the compiler says (if anything).

    If the message is something along the lines of "cannot convert void* to type*", then stop compiling your C code with a C++ compiler.
    FAQ > Casting malloc - Cprogramming.com

    > scanf("%s %d", &(ptr+i)->name, &(ptr+i)->marks);
    There's no need to go changing notation just because you made a dynamic allocation.

    scanf("%s %d", ptr[i].name, &ptr[i].marks);

    Nor is there any need to have an & when scanning %s into name, since name is already an array.

    In fact, when you get around to this
    Code:
    struct student{
      int marks;
      char *name;
    };
    Using &ptr[i].name is most definitely wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by Salem View Post
    > ptr = (struct student*) malloc (record * sizeof(struct student));
    Remove the cast, compile it, and tell us what the compiler says (if anything).
    Code:
    #include<stdio.h>
    #include <string.h>
    #include<stdlib.h>
    
    
    struct student{
      int marks;
      char name[10];
    };
    int main()
    {
        struct student *ptr;
        int i, record = 3;
       
        ptr =  malloc (record * sizeof(struct student));
       
     
        for(i = 0; i < record; ++i)
       {
           printf("Enter name of the student and marks respectively:\n");
           scanf("%s %d", ptr[i].name, &ptr[i].marks);
       }
       
         printf("Displaying Information:\n");
    
    
       for(i = 0; i < record ; ++i)
           
           printf("%s\t%d\n", ptr[i].name, ptr[i].marks);
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C dynamic memory allocation
    By Romyo2 in forum C Programming
    Replies: 4
    Last Post: 06-03-2015, 07:04 AM
  2. Help me.. (Dynamic memory allocation )
    By Devil211272 in forum C Programming
    Replies: 11
    Last Post: 10-17-2012, 09:10 AM
  3. Dynamic Memory Allocation
    By schifers in forum C Programming
    Replies: 12
    Last Post: 05-14-2008, 01:49 PM
  4. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  5. Dynamic memory allocation
    By amdeffen in forum C++ Programming
    Replies: 21
    Last Post: 04-29-2004, 08:09 PM

Tags for this Thread