It's mandatory to deallocate the memory area which has created dynamically when the memory is no longer needed.
Why do we need to deallocate the dynamic memory?
When we try to create the dynamic memory, the memory will be created in the heap section.
If we don't deallocate the dynamic memory, it will reside in the heap section.It is also called memory leak.
It will reduce the system performance by reducing the amount of available memory.
Let's assume total heap size as K.
If we allocate N byte of memory dynamically, it will consume N bytes of memory in heap section.
When the particular piece of code executed M number of time, then M * N bytes of memory will be consumed by our program.
At some point in time (M * N > K), the whole heap memory will be consumed by the program it will result in the system crash due to low available memory.
So, it is programmers responsibility to deallocate the dynamic memory which is no longer needed.
How to deallocate the dynamic memory?
using free() function, we can deallocate the dynamic memory.
Syntax of free
Sum of N numbers. Get N from the user and use dynamic memory to store the inputs.
Double free is undefined
If we free the same pointer two or more time, then the behavior is undefined.
So, if we free the same pointer which is freed already, the program will stop its execution.