Under stdlib header C provides us 4 four special function which helps in allocating memory dynamically and freeing it.
- malloc()
- calloc()
- realloc()
- free()
The Malloc function
Syntax
The malloc function allocates contiguous memory location for the required number_of_bytes, returns a void pointer pointing to the base address of that allocated memory. A malloc returns a void pointer,why?because malloc does not know for what we allocated the memory (malloc does not know whether we are allocating memory for ints , floats or chars ).
Usage example
The (int*) is done for typecasting, it converts the void pointer returned by malloc function to a integer pointer (int*) .
We are then assigning the returned(and type casted) pointer to a integer point a. So a is now pointing to the base address of allocated memory .
Let's look a complete code that uses a malloc function
We are including a header file stdlib because this is where our functions are defined.
So,basically we want to input N numbers from the user and display them.
At line 9.
We are dynamically allocating n*4 bytes of memory (size of a int is 4 bytes) . If n=5 then the function allocates 20 bytes of memory are returns a void pointer which is type casted to an integer pointer which is later assigned to a integer pointer a.
It is advised to check if the required memory is allocated or not this can be done by using the fact that malloc returns a NULL if memory cannot be allocated.
Remember that these function allocates contiguous memory.
So if 'a' points to the base address then 'a+1' points to the immediate next location after a.
Also the line 21 is equivalent to
This is because pointers and Arrays are interchangeable in C.
Note that.
Line 29
will be discussed later, just remember to free the allocated memory if it is of no use .
The calloc Function
The calloc function too allocates memory and returns a void pointer to that location.Unlike the malloc function it takes two arguments number_of_element and number_of_bytes.
The above line of code is equivalent to
However there is another difference between malloc and calloc function . It is,the memory allocated by calloc function initializes to zero.
Let us look at a complete example to demonstrate the use of calloc function
The functionality is similar to the the former program instead we are just using calloc function here.
The Realloc Function
Realloc is used to change (increase or decrease) the size of memory pointed by '*ptr' and changes it to 'number_of_bytes' . The newly initialized memory remains uninitialized.
If ptr is NULL then the call will be equivalent to
if number_of_bytes is equal to zero it is equivalent to
Note that only previously allocated memory can be reallocated.
A program to demonstrate the use of realloc function
In the above code we allocated certain bytes of memory using calloc function . However we came to know that the previously allocated memory was not enough for us, so we used the realloc function to reallocate the required size of memory.
The flow of above program goes like this:
We ask the user ,the total number he wants to input and then we allocate the required byte of memory using calloc function and we again prompt the user asking if he/she wants to add more number if his answer is yes we ask how many more and reallocate the memory using realloc function .
The free function
The above line of code causes the program to give back the block to the heap.
Although we have freed ptr it is not NULL it still contains some garbage value.
Memory Leak
The memory allocated by malloc function is not in any use but it is not released which causes memory leak.
It is advised to free all the allocated memory after it is of no use.
Clearly explained brother;)
ReplyDeleteKeep up
it is too difficult to find out the memory leak🙂
ReplyDelete