Skip to main content

Section 22.8 Arrays

Often we need to allocate space to store more than one value. Perhaps we need to allocate space for 1000 integers or 200 Person objects. In C++, we can use arrays to achieve this. Think of an array as a block of memory (contiguous) that can hold multiple values of the same type.
To dynamically allocate an array of data, we put the size we want in β€œsquare brackets” after the type ([size]). The individual elements will be initialized by the default constructor if they are objects or uninitialized if they are primitive values (int, double, char, etc...). To initialize an array of primitive values all to 0, we can use an empty initializer list after the brackets:
Listing 22.8.1.
// Allocate an array of 4 integers - uninitialized
int* data = new int[4];

// Allocate an array of 1000 integers - initialize to 0
int* data = new int[1000] {};

int numPersons = 10;
// Allocate an array of 10 Person objects - initialized with default constructor
Person* persons = new Person[numPersons];
The first line says something like β€œAllocate enough memory to hold 4 integers and give me a pointer to where the data starts.”. A memory diagram after running just that line in main might look like:
data points at a block of memory that holds four integers. The integers have random values.
Notice that the pointer data is a local variable in main’s stack frame. It points to the first element of the array, which is stored on the heap. The rest of the elements are stored in contiguous memory locations after the first element. All of the values are shown as random values because we did not initialize them.
To free the memory allocated for an array, we use the delete[] operator:
delete[] data;

Warning 22.8.1.

The [] is important. It says that we are deleting an array, not just a single value. Forgetting the brackets is an error that can lead to undefined behavior (but fortunately, AddressSanitizer will catch this).
Listing 22.8.2.

Checkpoint 22.8.1.

What happens if you modify the delete[] in the program above to delete?
  • The program fails to compile.
  • The program produces a random result.
  • AddressSanitizer produces a warning that specifies mismatched-new-delete.
  • AddressSanitizer produces a warning that specifies invalid-delete-address.

Checkpoint 22.8.2.

Allocate an array of 10 doubles on the heap, initialized to 0. Store the address into quizScores. You will not use all the blocks.

Note 22.8.2.

Arrays can also be allocated on the stack as a local variable. To do that, we don’t use new and we put the size of the array after the variable name:
int data[1000];
Arrays placed on the stack have a major limitation: the size of an array put on the stack must be known at compile time (i.e. a literal number or constant expression). So you can’t use a variable to declare the size. Trying to write int data[dataCount] where dataCount is a variable is a compile error as the compiler does not know how much space to allocate.
Plain arrays are a very low-level way of working with memory. In plain C programming, there are no vectors available, so they are the only tool available for storing a list of items. In C++, we have the option to use the Standard Template Library (STL) which provides higher-level abstractions like std::vector and std::array.
In this book we will only be using heap allocated arrays.
You have attempted of activities on this page.