|
Dynamic array allocation is actually a combination of pointers and dynamic memory allocation.While static arrays are declared prior to runtime and are reserved in stack memory, dynamic arrays are created in the heap and released from the heap and in C++ this is handled by using the new[ ] and delete[ ] operators.
int *example;
This C++ statement simply declares an integer pointer. A pointer is a variable that holds a memory address. Declaring a pointer doesn't reserve any memory for the array and that will be accomplished with new[ ].
The following C++ statement requests 15 integer-sized elements be reserved in the heap with the first element address being assigned to the pointer example:
example = new int[15];
The new[ ] operator is requesting 15 integer elements from the heap.Dynamic array allocation is nice because the size of the array can be determined at runtime and then used with the new[ ] operator to reserve the space in the heap.
|