Memory Allocation
In this chapter we will be discussing about different ways of allocating memory in c language.
C provide us the flexibility to allocate memory dynamically at run time in case we are not sure of size of memory required at the time of compilation of program.
C provides various functions to allocate memory dynamically . we can find all these functions declaration in <stdlib.h> header file.
1)void * malloc(int num_of_bytes):- malloc is used to allocate memory for number of bytes specified by num_of_bytes. It return the address or pointer to first byte of the allocated block. type of pointer returned by the malloc is void so it means we can type cast the pointer to any data type and can store data of any data type.
2) void * calloc (int num ,int size) :- calloc is used to allocate the memory block which contains num elements each of which size in bytes will be SIZE.
3) void *realloc(void *address,new_size):- this call extend the size of block pointed by address to the new_size.
4) void free(void *address) :- this function is used to free the whole memory pointed by address.
Example:-
suppose you want to multiply n elements entered by user. But you don’t know the value of n or we can say you don’t know how many elements will be entered by user.
because user will specify the value of n at run time. In this case we can allocate memory statically while coding the program because the memory allocated by us at the time of coding may be insufficient to store all the elements entered by user. So to solve this problem we allocate memory dynamically after receiving the value of n from user at run time. below is the example in which we are accepting n integer values from user and multiplying all the values.
#include<stdlib.h> //include the declaration of calloc , malloc and other memory related functions
int main()
{
int n,*ptr,i=0,result=1;
printf(“enter the number of elements”);
scanf(“%d”,n);
ptr=(int*)malloc(n*sizeof(int)); //memory allocation by malloc
for(i=0;i<n;i++)
{
result=result*(*ptr);
ptr++;
}
free(ptr); //free the allocated memory
printf(“result of multiplication is =%d”,result);
}
In the above program we allocated the memory by using malloc . we can also allocate memory by calloc instead of using malloc by following change in above program .
ptr=(int*)calloc(n,sizeof(int));
if at any point of time we feels that memory allocated by malloc or calloc is not sufficient and we need to extend it then we can do this by using realloc call.
#include<stdlib.h> //include the declaration of calloc , malloc and other memory related functions
int main()
{
int n,*ptr,i=0,result=1;
#include<stdlib.h> //include the declaration of calloc , malloc and other memory related functions
int main()
{
int n,*ptr,i=0,result=1;
printf(“enter the number of elements”);
scanf(“%d”,n);
ptr=(int*)malloc(n*sizeof(int)); //memory allocation by malloc
if(ptr==NULL)
{
printf(“memory allocation failed\n”);
return 1;
}
ptr=realloc(ptr,(n+2)sizeof(int)); //extend the size of block from n to n+2.
for(i=0;i<n;i++)
{
result=result*(*ptr);
ptr++;
}
free(ptr); //free the allocated memory
printf(“result of multiplication is =%d”,result);
}
ptr=(int*)malloc(n*sizeof(int)); //memory allocation by malloc
for(i=0;i<n;i++)
{
result=result*(*ptr);
ptr++;
}
free(ptr); //free the allocated memory
printf(“result of multiplication is =%d”,result);
}