What is Dynamic Memory Allocation in C?
The process of allocating memory during program execution is called dynamic memory allocation. Dynamic memory allocation in C refers to performing manual memory management for dynamic memory allocation using a group of functions in the C standard library, namely malloc, realloc, calloc and free. In Dynamic memory allocation in C, all local variables are allocated on the stack. They are freed automatically when the block ends. The Malloc() can be used to allocate memory from the heap and the memory allocated on the heap does not get freed automatically. It is freed only when the program calls free().
Let’s look at what the malloc() function can and cannot do:
- Used to allocate space in memory during the execution of a program.
- Does not initialize the memory allocated during execution. It carries garbage value.
- Returns null pointer if it is not able to allocate requested size of memory.
Got a question for us? Please mention them in the comments section and we will get back to you.