Dynamic Memory Allocation in C
- What is Dynamic Memory Allocation?
Runtime memory allocation is called dynamic memory allocation.
2. Why Dynamic Memory?
A. Efficient use of memory by saving waste memory.
B. To overcome Array size limitation.
C. To create new data structure using linked list, graph, stack and queue.
3. How can be performed dynamic memory allocation in C.
A. using malloc() function
B. using realloc() function
C. using calloc() function
4. How can be free used memory?
A. using free() function
5. Use of malloc() function
A. it take one parameter as size of memory allocation.
B. it return void * pointer (generic: it may int, float, long, char and etc. )
C. void * malloc(size_t size);
For Example:
int *p=(int *) malloc(5*sizeof(int)); //20 bytes
//p pointer have base or starting address of that allocated memory
Let’s take program to understand it:
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char const *argv[])
{
int num;
printf(“Enter size for data”);
scanf(“%d”,&num);
int *data=(int *)malloc(num*sizeof(int));
printf(“Enter 5 element in array”);
for(int i=0;i<num;i++)
{
scanf(“%d”,data+i);
}
printf(“Value after inserting is=”);
for(int j=0;j<num;j++)
{
printf(“%d “,*(data+j));
}
free(data);
return 0;
}
6. Use of realloc() function
A. it use to reallocate memory to older pointer if required without deleting existing values.
B. it take two parameter, one as old pointer and second as re-size
C. it return void* pointer.
D. void* realloc(void *oldpointer,size_t re-size);
For Example:
int *newp =(int *)realloc(p,7*sizeof(int));
Let’s take program to understand it:
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char const *argv[])
{
int num,num1;
printf(“Enter size for data”);
scanf(“%d”,&num);
int *data=(int *)malloc(num*sizeof(int));
printf(“Enter 5 element in array”);
for(int i=0;i<num;i++)
{
scanf(“%d”,data+i);
}
printf(“Value after inserting is=”);
for(int j=0;j<num;j++)
{
printf(“%d “,*(data+j));
}
printf(“\nEnter re-size for data again”);
scanf(“%d”,&num1);
data =(int *)realloc(data,num1*sizeof(int));
printf(“\nEnter remaining values”);
for(int i=num;i<num1;i++)
{
scanf(“%d”,data+i);
}
printf(“\nValue after re-size are=”);
for(int j=0;j<num1;j++)
{
printf(“%d “,*(data+j));
}
free(data);
return 0;
}
7. Use of calloc() function
A. it is use to allocate block of memory at runtime.
B. All blocks are initialized with zero.
C. it takes two parameters first number of block and second size of one block.
D. void * calloc(number of block, size of one block);
For Example:
int *p =(int *)calloc(5,sizeof(int));
Let’s take program to understand it:
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char const *argv[])
{
int size;
int size2;
printf(“Enter size of block”);
scanf(“%d”,&size);
int *data=(int *)calloc(size,sizeof(int));
printf(“\nDefault value of all block are”);
for(int j=0;j<size;j++)
{
printf(“%d “,*(data+j));
}
printf(“\nEnter %d value”,size);
for(int i=0;i<size;i++)
{
scanf(“%d”,data+i);
}
printf(“\nValue of calloc array is=”);
for(int j=0;j<size;j++)
{
printf(“%d “,*(data+j));
}
free(data);
return 0;
}