Free Function ( Dynamic Memory Allocation Part - 5 )

Definition

Compile time storage of a variable is allocated and is released after being used. But in the case of dynamical memory allocation as the memory is stored in the stack it is the responsibility of the user to free the memory space occupied by malloc or calloc functions after completing their operation. Storage is scarce so freeing memory when it is not being used is very important. So comes the free function.

Syntax of the free function

free ( pointer );
 Some important points to remember of the free function :
  • The pointer which has been originally used to store the malloc or calloc adresses must be used. Using invalid pointers will return a null pointer which may result in crashing of the system.
  • It is not the pointer that is being released, it the memory at which the pointer points to. The pointer is stored in the stack and will be erased after completion of the function where pointer is a local variable. 
  • To release an array of memory in the calloc we need to free the memory only one. Each individual element cannot be freed one at a time as it may cause error.

A program using calloc function :

Comments

Popular posts from this blog

Bubble Sort ( C & Python 3)

Comparison Logical and Bitwise Operator ( Java Part - 4 )

Something about me