C / Interview Questions / Unix
Dynamic Memory Allocation Explained With Simple Example
Dynamic memory allocation is the process of allocating memory at run time by the program based on the need of the program. Dynamic Memory allocation is done from the heap memory available in the system and handling of memory (creation and deletion) is totally handled by the programmer. In case, programmer forgets to cleanup the allocated dynamic memory block, it will lead into memory leak and this memory block will be blocked for further use until program restarts.
Methods available to allocate dynamic memory at run time are
- malloc () – allocates one memory block of requested size.
- calloc () – allocates multiple blocks of memory and initialize it with 0.
- realloc () – increases or decreases the size of previously allocated memory block.
- free () – frees the memory allocated by above functions.