
- 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
What do you mean by Dynamic memory allocation in C programming?
Dynamic Memory Allocation
Allocation of memory at the time of execution (run time) is known as dynamic memory allocation.
The functions calloc() and malloc() support allocating of dynamic memory.
Dynamic allocation of memory space is done by using these functions when value is returned by functions and assigned to pointer variables.
In this case, variables get allocated only if your program unit gets active.
It uses the data structure called heap for implementing dynamic allocation.
There is memory reusability and memory can be freed when not required.
It is more efficient.
In this memory allocation scheme, execution is slower than static memory allocation.
Here memory can be released at any time during the program.
Example
Following program computes the sum of even numbers and odd numbers in a set of elements using dynamic memory allocation functions −
#include<stdio.h> #include<stdlib.h> void main(){ //Declaring variables, pointers// int i,n; int *p; int even=0,odd=0; //Declaring base address p using malloc// p=(int *)malloc(n*sizeof(int)); //Reading number of elements// printf("Enter the number of elements : "); scanf("%d",&n); /*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); } //Storing elements into location using for loop// printf("The elements are :
"); for(i=0;i<n;i++){ scanf("%d",p+i); } for(i=0;i<n;i++){ if(*(p+i)%2==0){ even=even+*(p+i); } else{ odd=odd+*(p+i); } } printf("The sum of even numbers is : %d
",even); printf("The sum of odd numbers is : %d
",odd); }
Output
Enter the number of elements : 4 The elements are : 35 24 46 12 The sum of even numbers is : 82 The sum of odd numbers is : 35
- Related Articles
- What do you mean by static memory allocation in C programming?
- What is Dynamic Memory Allocation in C?
- Example program on Dynamic memory allocation in C language
- Explain dynamic memory allocation in C with an example
- What do you mean by a dynamic initialization of variables?
- Example program on Dynamic memory allocation function in C language
- Explain the dynamic memory allocation of pointer to structure in C language
- What do you mean by C++ Tokens?
- What do you mean by buffer in C language?
- What do you mean by function return in C language?
- What do you mean by odd loops in C language?
- What do you mean by starch?
- What do you mean by adolescence?
- What do you mean by heat?
- What do you mean by Nutrition?
