Memory allocation in c programmming


Memory allocation in c...


  • C  provide features to manual managements of memory, by   using this features we can manage memory at run time,.
  • whenever we require memory allocation or reallocation at run  time by using Dynamic Memory Allocation functions we can create amount of required memory.


There are following functions:



  • malloc - It is used to allocate specified number of bytes  .
  • calloc  - It is used to allocate specified number of bytes and     initialize all memory with 0.
  • reaclloc - It is used to reallocate the dynamically allocated   memory to increase or decrease amount of the memory.
  • free    - It is used to release dynamically allocated memory.


Dynamic Memory Allocation 

01        C programs to create memory for int, char and float variable            at  run time.

        here, In this program we will create memory for int, char                  and float variables at run time using malloc() function and                before exiting the program we will release the memory                    allocated at run time by using free() function.


           /*C program to create memory for int,char and float variable at run                          time.*/


  1.   #include <stdio.h>
  2.   #include <stdlib.h>
  3.  
  4.     int main()
  5.    {
  6.     int *iVar;
  7.     char *cVar;
  8.     float *fVar;
  9.      
  10.     /*allocating memory dynamically*/
  11.      
  12.     iVar=(int*)malloc(1*sizeof(int));
  13.     cVar=(char*)malloc(1*sizeof(char));
  14.     fVar=(float*)malloc(1*sizeof(float));
  15.      
  16.     printf("Enter integer value: ");
  17.     scanf("%d",iVar);
  18.      
  19.     printf("Enter character value: ");
  20.     scanf(" %c",cVar);
  21.      
  22.     printf("Enter float value: ");
  23.     scanf("%f",fVar);
  24.      
  25.     printf("Inputted value are: %d, %%.2f\n",*iVar,*cVar,*fVar);
  26.      
  27.     /*free allocated memory*/
  28.     free(iVar);
  29.     free(cVar);
  30.     free(fVar);
  31.  
  32.     return 0;
  33.    }

  OUTPUT:

         Enter integer value: 200
       Enter character value: x
       Enter float value: 555.76
       Inputted value are: 200, x, 555.76



02       C program to input and print texts using Dynamic Memory  .
        In this program we will create memory for text strings at run            time using malloc() function,
        text string will be inputted by the user and displayed. Using              free() function we will release the occupied memory.

          /*C program to input and print text using Dynamic Memory                 Allocation.*/


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.     int n;
  7.     char *text;
  8.      
  9.     printf("Enter limit of the text: ");
  10.     scanf("%d",&n);
  11.      
  12.     /*allocate memory dynamically*/
  13.     text=(char*)malloc(n*sizeof(char));
  14.      
  15.     printf("Enter text: ");
  16.     scanf(" "); /*clear input buffer*/
  17.     gets(text);
  18.      
  19.     printf("Inputted text is: %s\n",text);
  20.      
  21.     /*Free Memory*/
  22.     free(text);
  23.      
  24.     return 0;
  25. }


           Enter limit of the text: 100
       Enter text: I am  from  jaipur.
       Inputted text is: I am  jaipur.


 03    C program to read  a 1D array, print sum of all                                    elements  along  with inputted  array  elements using                        Dynamic  Memory   Allocation.


  •   In this program we will allocate memory for one dimensional    array and print the array  elements along with sum of all     elements. Memory will be allocated in this program using          malloc() and released using free().


        /*C program to read a one dimensional array,
         print sum of all elements along with inputted array
         elements using Dynamic Memory Allocation.*/


  1.    #include <stdio.h>
  2.    #include <stdlib.h>
  3.  
  4.     int main()
  5.      {
  6.     int *arr;
  7.     int limit,i;
  8.     int sum=0;
  9.      
  10.     printf("Enter total number of elements: ");
  11.     scanf("%d",&limit);
  12.      
  13.     /*allocate memory for limit elements dynamically*/
  14.     arr=(int*)malloc(limit*sizeof(int));
  15.      
  16.     if(arr==NULL)
  17.     {
  18.         printf("Insufficient Memory, Exiting... \n");
  19.         return 0;
  20.     }
  21.      
  22.     printf("Enter %d elements:\n",limit);
  23.     for(i=0; i<limit; i++)
  24.      {
  25.         printf("Enter element %3d: ",i+1);
  26.         scanf("%d",(arr+i));
  27.         /*calculate sum*/
  28.         sum=sum + *(arr+i);
  29.      }
  30.      
  31.     printf("Array elements are:");
  32.     for(i=0; i<limit; i++)
  33.         printf("%3d ",*(arr+i));
  34.      
  35.      
  36.     printf("\nSum of all elements: %d\n",sum);
  37.      
  38.     return 0;    
  39.     }

    Enter total number of elements: 5
    Enter 5 elements:
    Enter element 1: 11
    Enter element 2: 22
    Enter element 3: 33
    Enter element 4: 44
    Enter element 5: 55
    Array elements are: 11 22 33 44 55
    Sum of all elements: 165



04    .C program to read and print the student details using                        structure  and Dynamic Memory  Allocation.
        vIn this program we will create a structure with student                    details  and print the inputted details.Memory to store and                print structure will be allocated at run time by using malloc()            and   released  by free().
 

   /*C program to read and print the student details
     using structure and Dynamic Memory Allocation.*/
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /*structure declaration*/
  5. struct student
  6. {
  7.     char name[30];
  8.     int roll;
  9.     float perc;
  10. };
  11.  
  12. int main()
  13. {
  14.     struct student *pstd;
  15.      
  16.     /*Allocate memory dynamically*/
  17.     pstd=(struct student*)malloc(1*sizeof(struct student));
  18.      
  19.     if(pstd==NULL)
  20.     {
  21.         printf("Insufficient Memory, Exiting... \n");
  22.         return 0;
  23.     }
  24.      
  25.     /*read and print details*/
  26.     printf("Enter name: ");
  27.     gets(pstd->name);
  28.     printf("Enter roll number: ");
  29.     scanf("%d",&pstd->roll);
  30.     printf("Enter percentage: ");
  31.     scanf("%f",&pstd->perc);
  32.      
  33.     printf("\nEntered details are:\n");
  34.     printf("Name: %s, Roll Number: %d, Percentage: %.2f\n",pstd->name,pstd->roll,pstd->perc);
  35.       
  36.     return 0;
  37. }


    Enter name: pappu
    Enter roll number: 1
    Enter percentage: 87.50

    Entered details are:
    Name: pappu, Roll Number: 1, Percentage: 87.50



05     C program to read and print the N student details using                     structures and Dynamic Memory  Allocation.
         this program we will create a structure with N number of                 student details and print the  inputted details. Memory to                  store  and prints structure will be allocated at run time by                  using  malloc() and released by free().

       /*C program to read and print the N student
       details using structure and Dynamic Memory Allocation.*/



  1.  #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /*structure declaration*/
  5. struct student
  6. {#include <stdio.h>
  7. #include
  8.     char name[30];
  9.     int roll;
  10.     float perc;
  11. };
  12.  
  13. int main()
  14. {
  15.     struct student *pstd;
  16.     int n,i;
  17.      
  18.     printf("Enter total numbers of elements: ");
  19.     scanf("%d",&n);
  20.      
  21.     /*Allocated memory dynamically for n objetcs*/
  22.     pstd=(struct student*)malloc(n*sizeof(struct student));
  23.      
  24.     if(pstd==NULL)
  25.     {
  26.         printf("Insufficient Memory, Exiting... \n");
  27.         return 0;
  28.     }

   
                /*read and print details*/
                   for(i=0; i<n; i++)
            {
            printf("\nEnter detail of student [%3d]:\n",i+1);
            printf("Enter name: ");
            scanf(" "); /*clear input buffer*/
           gets((pstd+i)->name);
           printf("Enter roll number: ");
          scanf("%d",&(pstd+i)->roll);
          printf("Enter percentage: ");
          scanf("%f",&(pstd+i)->perc);
           }
   
        for(i=0; i<n; i++)
        {
        printf("%30s \t %5d \t %.2f\n",(pstd+i)->name,(pstd+i)->roll,                    (pstd+i)->perc);
         }
   
       return 0;
          }

      Enter total number of elements: 3

      Enter detail of student [1]:
      Enter name: Mike
      Enter roll number: 1
      Enter percentage: 87.50

      Enter detail of student [2]:
      Enter name:raju
      Enter roll number: 2
      Enter percentage: 65

      Enter detail of student [3]:
      Enter name: pappu
      Enter roll number: 3
      Enter percentage: 98.70

      Entered details are:
                             raju 1 65
            Jussy 2 88.00
                          pappu 3 98.70

No comments:

Post a Comment