Dynamic memory allocation

Dynamic memory allocation

Definition:
In C, there are two types of memory allocation: static and dynamic. Static allocation is the more basic of the two and is allocated to the Stack at execution.

Once allocated, static allocation has a fixed size. Static allocation is used for any global variables, file scope variables, and variables with the static keyword

Dynamic memory allocation is the more advanced of the two that can shift in size after allocation. This memory is stored in the Heap. Unlike the Stack, Heap memory has no variable size limitation.

Dynamic allocation allocates more memory as it’s needed, meaning that you never have more memory allocated than you need.

Dynamic memory allocation also has performance downsides. Cached heap data is not adjacent like cached Stack data. As a result, retrieving heap data is often less efficient than retrieving Stack data.



Posted on by