C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C programs

Dynamic Memory Allocation Programs/Examples in C Programming Language

Dynamic Memory Allocation in C Programming Language - C language provides features to manual management of memory, by using this feature 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 (memory blocks).
  • calloc - It is used to allocate specified number of bytes (memory blocks) 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.

In this section, we will learn programming using these function, here bunch of programs which are using Dynamic Memory Allocation.



Dynamic Memory Allocation Examples using C programs

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

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.*/

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *iVar;
    char *cVar;
    float *fVar;
    
    /*allocating memory dynamically*/
    
    iVar=(int*)malloc(1*sizeof(int));
    cVar=(char*)malloc(1*sizeof(char));
    fVar=(float*)malloc(1*sizeof(float));
    
    printf("Enter integer value: ");
    scanf("%d",iVar);
    
    printf("Enter character value: ");
    scanf(" %c",cVar);
    
    printf("Enter float value: ");
    scanf("%f",fVar);
    
    printf("Inputted value are: %d, %c, %.2f\n",*iVar,*cVar,*fVar);
    
    /*free allocated memory*/
    free(iVar);
    free(cVar);
    free(fVar);

    return 0;
}

    Enter integer value: 100
    Enter character value: x
    Enter float value: 123.45
    Inputted value are: 100, x, 123.45



2) C program to input and print text using Dynamic Memory Allocation.

In this program we will create memory for text string 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.*/

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    char *text;
    
    printf("Enter limit of the text: ");
    scanf("%d",&n);
    
    /*allocate memory dynamically*/
    text=(char*)malloc(n*sizeof(char));
    
    printf("Enter text: ");
    scanf(" "); /*clear input buffer*/
    gets(text);
    
    printf("Inputted text is: %s\n",text);
    
    /*Free Memory*/
    free(text);
    
    return 0;
}

    Enter limit of the text: 100
    Enter text: I am mike from California, I am computer geek.
    Inputted text is: I am mike from California, I am computer
    geek.



3) C program to read a one dimensional 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.*/

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *arr;
    int limit,i;
    int sum=0;
    
    printf("Enter total number of elements: ");
    scanf("%d",&limit);
    
    /*allocate memory for limit elements dynamically*/
    arr=(int*)malloc(limit*sizeof(int));
    
    if(arr==NULL)
    {
        printf("Insufficient Memory, Exiting... \n");
        return 0;
    }
    
    printf("Enter %d elements:\n",limit);
    for(i=0; i<limit; i++)
    {
        printf("Enter element %3d: ",i+1);
        scanf("%d",(arr+i));
        /*calculate sum*/
        sum=sum + *(arr+i);
    }
    
    printf("Array elements are:");
    for(i=0; i<limit; i++)
        printf("%3d ",*(arr+i));
    
    
    printf("\nSum of all elements: %d\n",sum);
    
	return 0;    
}

    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



4) C program to read and print the student details using structure and Dynamic Memory Allocation.

In 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.*/

#include <stdio.h>
#include <stdlib.h>

/*structure declaration*/
struct student
{
    char name[30];
    int roll;
    float perc;
};

int main()
{
    struct student *pstd;
    
    /*Allocate memory dynamically*/
    pstd=(struct student*)malloc(1*sizeof(struct student));
    
    if(pstd==NULL)
    {
        printf("Insufficient Memory, Exiting... \n");
        return 0;
    }
    
    /*read and print details*/
    printf("Enter name: ");
    gets(pstd->name);
    printf("Enter roll number: ");
    scanf("%d",&pstd->roll);
    printf("Enter percentage: ");
    scanf("%f",&pstd->perc);
    
    printf("\nEntered details are:\n");
    printf("Name: %s, Roll Number: %d, Percentage: %.2f\n",pstd->name,pstd->roll,pstd->perc);
     
    return 0;
}

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

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



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

In this program we will create a structure with N number of 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 N student 
details using structure and Dynamic Memory Allocation.*/

#include <stdio.h>
#include <stdlib.h>

/*structure declaration*/
struct student
{
    char name[30];
    int roll;
    float perc;
};

int main()
{
    struct student *pstd;
    int n,i;
    
    printf("Enter total number of elements: ");
    scanf("%d",&n);
    
    /*Allocate memory dynamically for n objetcs*/
    pstd=(struct student*)malloc(n*sizeof(struct student));
    
    if(pstd==NULL)
    {
        printf("Insufficient Memory, Exiting... \n");
        return 0;
    }
    
    /*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);
    }
    
    printf("\nEntered details are:\n");
    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: Jussy 
    Enter roll number: 2
    Enter percentage: 88

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

    Entered details are:
                              Mike 1 87.50
                             Jussy 2 88.00
                           Macalla 3 98.70





Advance C Programs
  1. C program to design flying characters Screen Saver.
  2. C program to design a TIC TAC TOE game.
  3. C program to check given string is a valid IPv4 address or not.
  4. C program to convert String into Hexadecimal.
  5. C program to demonstrate example of Variable Arguments.
  6. C program to store time in an integer variable.
  7. C program to store date in an integer variable.
  8. C program to find the size of a file in Linux.
  9. C program to remove consecutive repeated characters from string.
  10. C program to get Process Id and Parent Process Id in Linux.
  11. C program to get current System Date and Time in Linux.
  12. C program to create your own header file.
  13. EMI Calculator (C program to calculate EMI).
  14. Age Calculator (C program to calculate age).
  15. C program to design love calculator.
  16. C program to guess a random number.
  17. C program to validate date (Check date is valid or not).
  18. C program to format/extract ip address octets.
  19. C program to implement substring function.
  20. C program to design a digital clock.
  21. C program to convert hexadecimal Byte to integer.
  22. C program to extract bytes from an integer (Hexadecimal) value.
  23. C program to print character without using format specifiers.
  24. C program to find Binary Addition and Binary Subtraction.
  25. C program to print weekday of given date.
  26. C program to implement gotoxy(), clrscr(), getch(), getche() for GCC, Linux.
  27. C program to calculate compound interest.








COMMENTS