6.9a — Dynamically allocating arrays

In addition to dynamically allocating single values, we can also dynamically allocate arrays of variables. Unlike a fixed array, where the array size must be fixed at compile time, dynamically allocating an array allows us to choose an array length at runtime.

To allocate an array dynamically, we use the array form of new and delete (often called new[] and delete[]):

Because we are allocating an array, C++ knows that it should use the array version of new instead of the scalar version of new. Essentially, the new[] operator is called, even though the [] isn’t placed next to the new keyword.

Note that because this memory is allocated from a different place than the memory used for fixed arrays, the size of the array can be quite large. You can run the program above and allocate an array of length 1,000,000 (or probably even 100,000,000) without issue. Try it! Because of this, programs that need to allocate a lot of memory in C++ typically do so dynamically.

Dynamically deleting arrays

When deleting a dynamically allocated array, we have to use the array version of delete, which is delete[]. This tells the CPU that it needs to clean up multiple variables instead of a single variable. One of the most common mistakes that new programmers make when dealing with dynamic memory allocation is to use delete instead of delete[] when deleting a dynamically allocated array. Using the scalar version of delete on an array will result in undefined behavior, such as data corruption, memory leaks, crashes, or other problems.

Dynamic arrays are almost identical to fixed arrays

In lesson 6.8 -- Pointers and arrays, you learned that a fixed array holds the memory address of the first array element. You also learned that a fixed array can decay into a pointer that points to the first element of the array. In this decayed form, the length of the fixed array is not available (and therefore neither is the size of the array via sizeof()), but otherwise there is little difference.

A dynamic array starts its life as a pointer that points to the first element of the array. Consequently, it has the same limitations in that it doesn’t know its length or size. A dynamic array functions identically to a decayed fixed array, with the exception that the programmer is responsible for deallocating the dynamic array via the delete[] keyword.

Initializing dynamically allocated arrays

If you want to initialize a dynamically allocated array to 0, the syntax is quite simple:

Prior to C++11, there was no easy way to initialize a dynamic array to a non-zero value (initializer lists only worked for fixed arrays). This means you had to loop through the array and assign element values explicitly.

Super annoying!

However, starting with C++11, it’s now possible to initialize dynamic arrays using initializer lists!

Note that this syntax has no operator= between the array length and the initializer list.

For consistency, in C++11, fixed arrays can also be initialized using uniform initialization:

One caveat, in C++11 you can not initialize a dynamically allocated char array from a C-style string:

If you have a need to do this, dynamically allocate a std::string instead (or allocate your char array and then strcpy the string in).

Also note that dynamic arrays must be declared with an explicit length:

Resizing arrays

Dynamically allocating an array allows you to set the array length at the time of allocation. However, C++ does not provide a built-in way to resize an array that has already been allocated. It is possible to work around this limitation by dynamically allocating a new array, copying the elements over, and deleting the old array. However, this is error prone, especially when the element type is a class (which have special rules governing how they are created).

Consequently, we recommend avoiding doing this yourself.

Fortunately, if you need this capability, C++ provides a resizable array as part of the standard library called std::vector. We’ll introduce std::vector shortly.


1) Write a program that:
* Asks the user how many names they wish to enter.
* Asks the user to enter each name.
* Calls a function to sort the names (modify the selection sort code from lesson 6.4 -- Sorting an array using selection sort)
* Prints the sorted list of names.

Hint: Use a dynamic array of std::string to hold the names.
Hint: std::string supports comparing strings via the comparison operators < and >

Your output should match this:

How many names would you like to enter? 5
Enter name #1: Jason
Enter name #2: Mark
Enter name #3: Alex
Enter name #4: Chris
Enter name #5: John

Here is your sorted list:
Name #1: Alex
Name #2: Chris
Name #3: Jason
Name #4: John
Name #5: Mark

Quiz solutions

1) Show Solution

6.10 -- Pointers and const
6.9 -- Dynamic memory allocation with new and delete

223 comments to 6.9a — Dynamically allocating arrays

  • JRCS

    I seem to have got into the habit of putting everything in functions if I can, so this code looks quite different to your solution. It still works, so that’s cool, I suppose.

    • Alex

      Yup, that’s great! Your code is nicely organized. A few minor things:
      1) Using a global “using namespace std” is frowned upon
      2) You don’t need to set name to 0 after deleting it (it’s going to go out of scope and get destroyed immediately anyway)

  • chathura

    Oh thx for that it did help.

  • chathura

    Hi i just have a small problem with sortArray method. How does it sort the string alphabetically i’m bit confused because i understand the concept if they were numbers. please help.

  • Eric

    I have a question about the sorting function. When sorting, is it doing it by the ascii value, because i was playing around and mixing capitalization and lowercase and it appears to be arranging by those values. What would you suggest be the most optimal way to correct this? As an example run a check and convert everything to be uniformed (capitalized or all lowercased).

    • Alex

      Yes, if you’re sorting characters, it will do so by ASCII value. You can fix this by converting everything in your source array to use uniform capitalization. But a better idea is to modify your sort to deal with inconsistent capitalization (so caps gets temporarily converted to lower case before the comparison happens). That way, your source array can remain unmodified.

  • Angmar

    Hi Alex, thanks for your prior helps. I have another question.

    So the

    operator returns a pointer, right? Is it possible to dereference them with the

    operator? I was trying to build a string array and each of the following fails to build:

    I’d be very grateful if you explain why 1,2, and 6 won’t work

    • Alex

      When you dereference a pointer that is pointing to an array, you get the value of the first element.
      1) Doesn’t work because you’re trying to initialize your names array with a single value. This causes a type mismatch.
      2) Direct initialization doesn’t work with arrays, which is one of the reason uniform initialization was created.
      6) This is a confusing case. string names[] defines a string array (not a string pointer). new string[count] returns a pointer. C++ won’t let you convert a pointer into an array.

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter