Share this post on:

Dynamic Memory Allocation in C is the memory allocation that we have done till now was static memory allocation. The memory that could be used by the program was fixed that is we could not allocate or deallocate memory during the execution of the program. In many application it is not Possible to predict how much memory would be needed by the program at run time.

For example we declare an array of integer of size 200.

int emp_no[200];

In an array, it is must to specify the size of array while declaring, so the size of this array will be fixed during runtime. Now two type of problem may occur. The first case is that the number of values to be stored is less than the size of array, so there will be wastage of memory. for example if we have to store only 50 values in the above array, then space for 150 values (600 bytes ) is wasted. In second case our program fails if we want to store more values than the size of array, for example if there is need to store 205 values in the above array.

To overcome these problem we should be able to allocate and deallocate memory at run time. The process of allocating memory at the time of execution is called dynamic memory stdlib.h . These function allocate memory from a memory area called heap and deallocate this memory whenever not required, so that it can be used again for some other purpose.

pointers play an important role in dynamic memory allocation because we can access the dynamically allocated memory only through pointers.


Declaration of malloc() : void *malloc(size_t_size);

This function is used to allocate memory dynamically. The argument size specifies the number of bytes to be allocate. The type size_t is defined in stdlib.h and other headers as unsigned int. On success, malloc() returns a pointer to the first byte of allocated memory. The returned pointer is of type void, which can by type cast to appropriate type of pointer.

It is generally used as :- ptr =(datatype *)malloc(specified size);

here ptr is pointer of type datatype and specifies size is the size in bytes required to allocated. the expression (datatype *) is used to typecast the pointer by malloc().

For example :-

int *ptr;

ptr=(int *)malloc(12);

This allocates 12 contiguous bytes of memory space and the address of first byte is stored in the pointer variable ptr.


Declaration of calloc() : void *calloc(size_t n, size_t size);

The calloc() function is used to allocate multiple block of memory. It is similar to malloc() function except for two difference. The first one is that it takes two arguments. The first arguments specifies the number of blocks and the second one specifies the size of each block.

For example :- ptr=(int *)calloc(5,sizeof(int));

This allocates 5 block of memory, each block contains 4 bytes and the starting address is stored in the pointer variable ptr, which is of type int. An equivalent malloc() call would be – ptr=(int *)calloc(5*sizeof(int));

The other difference between calloc() and malloc() is that the memory allocated by malloc() contains garbage value while the memory allocated by calloc() in initialized to zero. but this initialization by calloc() is not very reliable, so it is better to explicitly initialize the elements whenever there is need to do so.

Like malloc(), calloc() also returns NULL if there is not sufficient memory available in the heap.


Declaration of realloc() :- void *realloc(void *ptr, size_t newsize)

We may want to increase or decrease the memory allocated by malloc() or calloc(). The function realloc() is used to change the size of the memory block. It alters the size of the memory block. It alters the size of the memory block without losing the old data. This is known as reallocation of memory. This function takes two arguments, first is a pointer to the block of memory that was previously allocated by malloc() or calloc() and second one is the new size for that block.

For example :- ptr=(int *)malloc(size);

This statement allocated the memory of the starting address of this memory block is stored in the pointer variable ptr. If we want to change the size of this memory block, then we can use realloc() as :- ptr=(int *)realloc(otr,newsize);

This statement allocated the memory space of newsize bytes, and the starting address of this memory block is stored in the pointer variable ptr. the newsize may be smaller or larger than the old size. if the newsize is larger, then the old data is not lost and the newly allocated bytes are uninitialized. The starting address contained in ptr may change if there is not sufficient memory at the old address to store all the bytes consecutively. This function moves the contents of old block into the new block and the data of the old block is not lost. On failure, realloc() returns NULL and is this case the old memory is not deallocated and it remains unchanged.

If ptr is a null pointer, realloc() behaves like malloc() function. If ptr is not a pointer returned by malloc(), calloc() or realloc(), the behavior is undefined.


Declaration of free() is : void free(void *p);

The dynamically allocated memory is not automatically released, it will exist till the end of program. If we have finished working with memory allocated dynamically, it is our responsibility to release that memory so that it can be reused. The function free() is used to release the memory space allocated dynamically. The memory released by free() is made a available to the heap again and can be used for some other purpose.


Here ptr is a pointer variable that contains the base address of a memory block created by malloc() or calloc(). one a memory location is freed it should not be used. we should not try to free any memory location that was not allocated by malloc(), calloc() or realloc().

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

One Comment

  1. Pingback: What is Union in C Programming - ArzaTechs

Leave a Comment

Your email address will not be published. Required fields are marked *