Dynamic Arrays

Learn how to allocate memory to an array dynamically.

Dynamic arrays

In the arrays lesson, we discussed static arrays. In a static array, a fixed amount of memory is allocated to the array during the compile time. Therefore, we cannot allocate more memory to the arrays during program execution.

Suppose we have declared an array that can store five integer values.

What if we want to store more than five values in an array? Here is dynamic arrays come in!


Dynamic arrays can grow or shrink during the program execution.

Declaration

The general syntax for declaring dynamic arrays is given below:

Get hands-on with 1400+ tech skills courses.