Tags

C Programming Dynamic Memory Allocation

The exact size of array is unknown untill the compile time,i.e., time when a compier compiles code written in a programming language into a executable form. The size of array you have declared initially can be sometimes insufficient and sometimes more than required. Dynamic memory allocation allows a program to obtain more memory space, while running or to release space when no space is required.
Although, C language inherently does not has any technique to allocated memory dynamically, there are 4 library functions under “stdlib.h” for dynamic memory allocation.

Function Use of Function
malloc() Allocates requested size of bytes and returns a pointer first byte of allocated space
calloc() Allocates space for an array elements, initializes to zero and then returns a pointer to memory
free() dellocate the previously allocated space
realloc() Change the size of previously allocated space

malloc()
The name malloc stands for “memory allocation”. The function malloc() reserves a block of memory of specified size and return a pointer of type void which can be casted into pointer of any form.
Syntax of malloc()
ptr=(cast-type*)malloc(byte-size)
Here, ptr is pointer of cast-type. The malloc() function returns a pointer to an area of memory with size of byte size. If the space is insufficient, allocation fails and returns NULL pointer.
ptr=(int*)malloc(100*sizeof(int));
This statement will allocate either 200 or 400 according to size of int 2 or 4 bytes respectively and the pointer points to the address of first byte of memory.
// sum of n elements using malloc
// author : Prof.Prasad Sawant
#include
#include
#include
void main()
{
int n,i,*ptr,sum=0;
clrscr();
printf(“How many number of elements do u want ?”);
scanf(“%d”,&n);
ptr=(int *)malloc(n* sizeof(int));
if(ptr==NULL)
{
printf(“Error !!!!!!”);
exit(0);

}
printf(“\n Enter elements in array”);
for(i=0;i<n;i++)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum is %d ",sum);
free(ptr);

getch();
}

calloc()
The name calloc stands for "contiguous allocation". The only difference between malloc() and calloc() is that, malloc() allocates single block of memory whereas calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero.
// sum of n elements using malloc
// author : Prof.Prasad Sawant

#include
#include
#include
void main()
{
int n,i,*ptr,sum=0;
clrscr();
printf(“How many number of elements do u want ?”);
scanf(“%d”,&n);
ptr=(int *)calloc(n,sizeof(int));
if(ptr==NULL)
{
printf(“Error !!!!!!”);
exit(0);

}
printf(“\n Enter elements in array”);
for(i=0;i<n;i++)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum is %d ",sum);
free(ptr);

getch();
}

free()
Dynamically allocated memory with either calloc() or malloc() does not get return on its own. The programmer must use free() explicitly to release space.
syntax of free()
free(ptr);
realloc()
If the previously allocated memory is insufficient or more than sufficient. Then, you can change memory size previously allocated using realloc().
Syntax of realloc()
#include
#include
int main(){
int *ptr,i,n1,n2;
printf(“Enter size of array: “);
scanf(“%d”,&n1);
ptr=(int*)malloc(n1*sizeof(int));
printf(“Address of previously allocated memory: “);
for(i=0;i<n1;++i)
printf("%u\t",ptr+i);
printf("\nEnter new size of array: ");
scanf("%d",&n2);
ptr=realloc(ptr,n2);
for(i=0;i<n2;++i)
printf("%u\t",ptr+i);
return 0;
}