...

/

Dynamically Allocated Memory

Dynamically Allocated Memory

Learn how to control the amount of memory allocated for a variable on a heap by using some built-in C functions.

Languages like MATLAB, Python, etc, allow us to work with data structures like arrays, lists, etc, that we can dynamically resize. That is to say, we can make them longer, shorter, etc, even after they are created. In C, this is not so easy.

Once we’ve allocated a variable such as an array on the stack, it is fixed in its size. We cannot make it longer or shorter. In contrast, if we use the functions malloc or calloc to allocate an array on the heap, we can use the function realloc to resize it later. In order to use these functions we need to include stdlib.h at the top of our C file.

The built-in ...