From today, I started to learn important concepts in computer languages. For today, I decided to learn dynamic memory allocation. It is used to allocate memory by the user itself based on the their requirement. It has four methods for allocating memory.
- malloc – This will create the memory in the Heap space in ram. We should pass the size as the argument. The return type is void so we must explicitly type cast the of pointer. For example int pointer will be (int*)malloc( 4 * sizeof(int) )
- calloc – Contiguous allocation, It is used to create 4 different space in memory. Example (int*)calloc(4, sizeof(int))
- realloc – It is used re allocate the space of memory in which the pointer has already assigned
- free – It is used to remove the memory allocated to the pointer. We should pass the allocated pointer as the argument.