Dynamic Memory Allocation in C
Dynamic Memory Allocation is manual allocation and freeing of
memory according to your programming needs. Dynamic memory is managed and
served with pointers that point to the newly allocated memory space in an area
which we call the heap.
Now you can create and destroy an array of elements dynamically
at runtime without any problems. To sum up, the automatic memory management
uses the stack, and the dynamic memory allocation uses the heap.
The <stdlib.h> library has functions responsible for
dynamic memory management.
The malloc Function
The malloc() function stands
for memory allocation. It is a function which is used to allocate a block of
memory dynamically. It reserves memory space of specified size and returns the
null pointer pointing to the memory location. The pointer returned is usually
of type void. It means that we can assign malloc function to any pointer.
Syntax
ptr=(cast-type*)
malloc(byte-size)
Program:
#include<stdlib.h>
int main()
{
int *ptr;
ptr=malloc(15 *
sizeof(*ptr));
if(ptr !=NULL)
{
*(ptr+5)=480;
printf(“value of the 6th
integer is %d”, *(ptr*5));
}
}
Output:
Value of the 6th
integer is 480
The calloc Function
The calloc function stands for contiguous allocation. This
function is used to allocate multiple blocks of memory. It is a dynamic memory
allocation function which is used to allocate the memory to complex data
structures such as arrays and structures.
Malloc function is used to allocate a single block of memory
space while the calloc function is used to allocate multiple blocks of memory
space. Each block allocated by the calloc function is of the same size.
Fig: calloc() |
Syntax:
ptr(cast-type*) calloc
(n,size);
Program:
#include<stdlib.h>
int main()
{
int i=ptr,sum=0;
ptr=calloc(10,sizeof(int));
if(ptr==NULL)
{
printf(“error! Memory
not allocated”);
exit(0);
}
printf(“Building and
calculating the sequence sum of the first 10 terms\n”);
for(i=0;i<10;i=+)
sum+=*(ptr+i);
}
printf(“sum= %d”,sum);
free(ptr);
return 0;
}
Output:
Building and
calculating the sequence sum of the first 10 terms
Sum=45
The realloc Function
The realloc() function, we can
add more memory size to already allocated memory. It expands the current block
while leaving the original content as it is. realloc stands for reallocation of
memory.
realloc can also be used to reduce the size of the previously
allocated memory.
Fig: realloc() |
Syntax:
ptr=realloc(ptr,newsize);
Program
#include<stdio.h>
int main()
{
char *ptr;
ptr=(char*)realloc(ptr , 20);
strcat(ptr, “In ‘C’ ”);
printf(“%s,address=%u\n”,ptr,ptr);
free(ptr);
return 0;
}
The free Function
The memory for variables is automatically deallocated at compile
time. In dynamic memory allocation, you have to deallocate memory explicitly.
If not done, you may encounter out of memory error.
The free() function is called to release/deallocate memory. By freeing memory in your program, you make more available for use later.
Fig: Free() |
Program
#include<stdio.h>
int main()
{
int*ptr=malloc(10*
sizeof(*ptr));
if(ptr !=NULL)
{
*(ptr+2)=50;
printf(“value of the 2nd
integer is %d “, *(ptr +2));
}
free(ptr);
}
Output:
Value of the 2nd
integer is 50
No comments:
Post a Comment