How To Use Dynamic Memory in C Malloc|Calloc|Realloc|Free

How To Use Dynamic Memory in C Malloc|Calloc|Realloc|Free
How To Use Dynamic Memory in C Malloc|Calloc|Realloc|Free

How To Use Dynamic Memory in C Malloc|Calloc|Realloc|Free 

Dynamic Memory Allocation: A dynamic memory allocation can be used to allow the allocation of memory at runtime.

Types of dynamic memory allocation.


There are Four types of Dynamic Memory Allocation.

1. Malloc() Dynamic Memory Allocation

2. Calloc Dynamic Memory Allocation 

3. Realloc Dynamic Memory Allocation 

4.free() Dynamic Memory Allocation 

the malloc(): A malloc dynamic memory stands for Memory Allocation.

it can be allocated and reserved a single block of memory of specified space size.

malloc can return NULL if memory is insufficient.

malloc function can return the address of a byte in the allocated memory if memory is sufficient.

Syntax of Malloc function


p=(cast-type*)malloc(byte-size)


Here p is the pointer,cast-type is the data type in which you can want to cast the returning pointer byte-size is the allocated memory space.

Example of Malloc function


p = (int*) malloc(50 * sizeof(int));


Here given statements allocate 100 or 200 bytes of memory. If the size of int is 2 then 50*2=100 bytes will be allocated, if the size of int is 4 then 50*4=200 bytes will be allocated. Pointer p holds the address of the first byte of allocated memory.

How To Use Malloc Function Dynamic Memory?

#include<stdio.h> 

#include<stdlib.h> 

int main() 


float sum=0,*p; 

int n; 

printf("Enter Number Add Memory:"); 

scanf("%d", &n);

p= (float*) malloc(n* sizeof(float)); 

if(p == NULL) 

{

printf("Insufficient space memory not available");

exit(0);

}

for(int i = 0; i < n; ++i) 


printf("Enter element:%d=",i+1); 

scanf("%f", p + i);

sum =sum+ *(p + i); 


printf("Total Sum of Element: %f", sum);


free(p); 

return 0; 



*****OUTPUT*****

Enter Number Add Memory:4 

Enter Element:1=4.1 

Enter Element:2= 6.5

Enter Element:3=5.7

Enter Element:4=3.9

Total Sum of Element:
22.199999



calloc(): A Calloc function can be used contiguous allocation. Contiguous memory allocates and reserved multiple blocks of memory of the same size.
A Call function can be allocated memory space is initialized to zero.
calloc function can return NULL if memory is insufficient.

Syntax of calloc function


p=(cast-type*)calloc(number,size)


Here p is the pointer,cast-type is the data type in which you can want to cast the returning pointer.

Example of calloc function


p = (float*) calloc(100 , sizeof(int)); 


Here statements allocate 100 blocks of memory space with the size of 2 bytes because the size of int is 2 bytes.

How To Use Calloc Function Dynamic Memory Allocation?

#include<stdio.h> 

#include<stdlib.h> 

int main() 


float sum=0,*p; 

int n; 

printf("Enter Number Add Memory:"); 

scanf("%d", &n);


p= (float*) calloc(n,sizeof(float)); 

if(p == NULL) 


print("Insufficient Space Memory not allocated."); 

exit(0);


for(int i = 0; i < n; ++i) 


printf("Enter Element:%d=",i+1); 

scanf("%f", p + i);


sum =sum+ *(p + i);


printf("Total Sum of Element:%f", sum);


free(p);

return 0; 

}

*****OUTPUT*****

Enter Number Add Memory:4 

Enter Element:1=4.1 

Enter Element:2= 6.5

Enter Element:3=5.7

Enter Element:4=3.9

Total Sum of Element:
22.199999


realloc(): A realloc function can be allocated memory space is insufficient then it is possible to modify the allocated space.

realloc function is used to modify the size of previously allocated memory.

realloc memory Allocation can be user modify memory space.

Syntax of realloc function


p = realloc(p, new size ); 


How To Use Realloc Function Dynamic Memory Allocation?

#include<stdio.h>

#include<stdlib.h>

int main()

{

int* p;

int n;

printf("Enter a size of Add Element:");

scanf("%d",&n);

ptr=(int*)calloc(n,sizeof(int));

for(int i=0;i<n;i++)

{

printf("Enter the Element:");

scanf("%d",&p[i]);

}

for(int i=0;i<n;i++)


printf("The Value at of this Array %d:%d\n",i,p[i]);


printf("\n\nEnter a New Size of Array:");


scanf("%d",&n);


ptr=(int*)realloc(p,n*sizeof(int));


for(int i=0;i<n;i++)

{

printf("Enter the Element:");


scanf("%d",&p[i]);


}

for(int i=0;i<n;i++)


printf("The Value at of this Array %d:%d\n",i,p[i]);


free(p);


return 0;


}

*****OUTPUT*****

Enter the size of Add Element:4 

Enter the Element:5 

Enter the Element:2 

Enter the Element:3
 
Enter the Element:5 

The Value at of this Array 0:5 

The Value at of this Array 1:2 

The Value at of this Array 2:3 

The Value at of this Array 3:5
 
Enter a New Size of Array:5 

Enter the Element:9 

Enter the Element:8 

Enter the Element:7 

Enter the Element:6 

Enter the Element:5 

The Value at of this Array 0:9 

The Value at of this Array 1:8 

The Value at of this Array 2:7
 
The Value at of this Array 3:6 

The Value at of this Array 4:5

free(): A free memory Allocation can be used to free the allocated memory space.

Syntax of realloc function


free(p);


How To Use Free Function Dynamic Memory Allocation?

#include<stdio.h> 

#include<stdlib.h> 

int main() 


float sum=0,*p; 

int n;
 
printf("Enter Number Add Memory:"); 

scanf("%d", &n);


p= (float*) calloc(n,sizeof(float)); 

if(p == NULL) 


printf("Insufficient Space Memory not allocated."); 

exit(0);


for(int i = 0; i < n; ++i) 

{
 
printf("Enter Element:%d=",i+1); 

scanf("%f", p + i);


sum =sum+ *(p + i);


printf("Total Sum of Element:%f", sum);


free(p);

return 0; 

}

*****OUTPUT*****

Enter Number Add Memory:4 

Enter Element:1=4.1 

Enter Element:2= 6.5

Enter Element:3=5.7

Enter Element:4=3.9

Total Sum of Element:
22.199999

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.