
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Explain dynamic memory allocation in C with an example
Problem
Find the sum of n numbers entered by user using dynamically allocated memory using C programming.
Solution
The Dynamic memory allocation enables the C programmers to allocate memory at runtime.
The different functions that we used to allocate memory dynamically at run time are −
- malloc () − allocates a block of memory in bytes at runtime.
- calloc () − allocating continuous blocks of memory at run time.
- realloc () − used for reduce (or) extend the allocated memory.
- free () − deallocates previously allocated memory space.
Following C program is to display the elements and calculate sum of n numbers.
Using dynamic memory allocation functions, we are trying to reduce the wastage of memory.
Example
#include<stdio.h> #include<stdlib.h> void main(){ //Declaring variables and pointers,sum// int numofe,i,sum=0; int *p; //Reading number of elements from user// printf("Enter the number of elements : "); scanf("%d",&numofe); //Calling malloc() function// p=(int *)malloc(numofe*sizeof(int)); /*Printing O/p - We have to use if statement because we have to check if memory has been successfully allocated/reserved or not*/ if (p==NULL){ printf("Memory not available"); exit(0); } //Printing elements// printf("Enter the elements :
"); for(i=0;i<numofe;i++){ scanf("%d",p+i); sum=sum+*(p+i); } printf("
The sum of elements is %d",sum); free(p);//Erase first 2 memory locations// printf("
Displaying the cleared out memory location :
"); for(i=0;i<numofe;i++){ printf("%d
",p[i]);//Garbage values will be displayed// } }
Output
Enter the number of elements : 5 Enter the elements : 23 34 12 34 56 The sum of elements is 159 Displaying the cleared out memory location : 12522624 0 12517712 0 56
- Related Articles
- Example program on Dynamic memory allocation in C language
- Example program on Dynamic memory allocation function in C language
- What is Dynamic Memory Allocation in C?
- Explain the dynamic memory allocation of pointer to structure in C language
- What do you mean by Dynamic memory allocation in C programming?
- Explain the concept of DYNAMIC SQL in DB2 with the help of an example
- ArduinoJSON: Memory Allocation
- Contiguous memory allocation
- How to restrict dynamic allocation of objects in C++?
- Dynamic Channel Allocation in computer network
- Assumptions for Dynamic Channel Allocation
- MCQ on Memory allocation and compilation process in C
- What is Image Array? Explain with an example in C++
- What do you mean by static memory allocation in C programming?
- Explain 3NF with an example in DBMS

Advertisements