Search

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.

Quiz

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
Index
6.9 -- Dynamic memory allocation with new and delete

243 comments to 6.9a — Dynamically allocating arrays

  • Jeffery

    I know it is unrelated to the lesson, but while I was experimenting with my program for the quiz, I was havin a problem with std::getline. It would not ask for user input in the first iteration of my for loop asking for names, but worked fine for the rest. This was when I entered 5. I later added     std::cin.ignore(32767, ‘n’); after the place where it asked for the number of names and it seemed to work fine, showing that there were too many inputs(at least I think). Do you mind looking over mycode to see why this may occur.

    void Sort(std::string *if, int length)
    {
        for (int startIndex = 0; startIndex < length - 1; ++startIndex)
        {

            int smallestIndex = startIndex;

            for (int currentIndex = startIndex + 1; currentIndex < length; ++currentIndex)
            {
                if (if[currentIndex] < if[smallestIndex])
                    smallestIndex = currentIndex;
            }

            std::swap(if[startIndex], if[smallestIndex]);
        }
    }

    int main()
    {
        int nameLength;

            std::cout << "How many names would you like to enter? ";
            std::cin >> nameLength;
        //    std::cin.ignore(32767, ‘n’); adding this makes it work as intended
        std::string *ptr = new std::string[nameLength] ();
        for (int i{ 0 }; i < nameLength; ++i)
        {
            std::cout << "Enter name #" << i + 1 << ": ";
            std::getline(std::cin, ptr[i]);
        

        }
        Sort(ptr, nameLength);
        for (int i{ 0 }; i < nameLength; ++i)
            std::cout << ptr[i];
    delete[] array;
    array = nullptr;
    }

    • nascardriver

      Hi Jeffery!
      Whenever you input something through std::cin it will be stored in a buffer internally.
      When the user inputs nameLength a ‘\n’ character will be in that buffer, because the user pressed enter (which is ‘\n’ or "\r\n").
      Now, when you call std::getline it will see that there’s a ‘\n’ in the buffer and it will do it’s thing, because it thinks the line is over.
      To fix this you can add this line

      This will ignore all input until to next ‘\n’, which is the ‘\n’ you received via std::cin.
      Note: std::numeric_limits<streamsize>::max() might not be available with old compilers or old settings, if you encounter problems you can replace it with a large number.

      Also, in your Sort function you have a variable named "if", don’t do this.

    • Alex

      When you ask how many names to enter, your input captures both the number entered and the newline character. Then when you call std::getline, it finds the newline character still in the input stream, and interprets that as an empty string. Calling std::cin.ignore() flushes the newline out of the stream so your code works as expected.

      I talk about this problem in lesson http://www.learncpp.com/cpp-tutorial/4-4b-an-introduction-to-stdstring/

  • Ben

    Hi, I’ve just finished the quiz and was hoping for a little feedback 🙂

    The code works fine and does what it should but it does differ from yours in the solution and was wondering if it would be an issue for optimisation etc. One question I do have, when checking the solution after completing the quiz I realised that initially I had forgot to include <utility> but the std::swap still seemed to work, why would this be the case?

    Thanks.

    • Alex

      Your code could use some comments defining the different sections of main helping clarify what the different sections do (user input, sort, print list). There’s also no need to set array to nullptr at the end of the function, since array will get destroyed just after that point.

      It’s possible one of the other headers you include included utility itself. You should still include it.

  • Bryan

    Hello Alex,

    While I understand how to make a dynamic array, it is not described how to allocate and use a multi-dimensional array.

    If I would make an array like:

    How does the pointer returned from the array work?
    Does it return multiple pointers for each of the starting elements per dimension?

    And if for example I would want to iterate through a multi-dimensional array using:

    how would I iterate that it goes to the next dimension of the array?
    with + length ? or using multiple pointers?

    I’m sorry for the multiple questions, I hope you are able to answer them!

    Thank you very much for your tutorials I really consider them the best around!

  • Mattermonkey

    If pointers to arrays don’t know how long the array is, how does delete[] know how much memory to deallocate?

    • Alex

      When you allocate dynamic memory using new[], the size of the allocated memory is stored, so that when delete[] is called, the proper amount can be deallocated. Unfortunately, this size isn’t accessible to the programmer.

  • DOG_TRAINER

    You said b4 that we need to use this, so our program doesn’t skip over after asking for a number then a string, but yours didn’t skip at all.

    Here’s my code: What do you think? Thank you.

  • Kushagra

    Why can’t we use std::getline to get names?
    what about getting sirnames  with std::cin?
    didn’t it put the sirname to the next std::cin ?

    • Alex

      > Why can’t we use std::getline to get names?

      You can, like this:

      > what about getting sirnames with std::cin?

      std::cin and operator<< break on whitespace. So you’d have to keep reading until you hit a newline and concatenate the results. If you want to do this, just use std::getline.

      • Kushagra

        using std::getline is causing run time error.
        name 1 is printed. without asking anything it also prints name 2 , then it asks about entering name

        • Alex

          Can you post your code snippet that’s not working?

          • Kushagra

            Your snippet is doing the same problem.

            #include <iostream>
            #include <string>
            #include <algorithm>

            void sortArray(std::string *array, int length)
            {
                // Step through each element of the array
                for (int startIndex = 0; startIndex < length; ++startIndex)
                {
                    // smallestIndex is the index of the smallest element we’ve encountered so far.
                    int smallestIndex = startIndex;

                    // Look for smallest element remaining in the array (starting at startIndex+1)
                    for (int currentIndex = startIndex + 1; currentIndex < length; ++currentIndex)
                    {
                        // If the current element is smaller than our previously found smallest
                        if (array[currentIndex] < array[smallestIndex])
                            // This is the new smallest number for this iteration
                            smallestIndex = currentIndex;
                    }

                    // Swap our start element with our smallest element
                    std::swap(array[startIndex], array[smallestIndex]);
                }
            }

            int main()
            {
                std::cout << "How many names would you like to enter? ";
                int length;
                std::cin >>length ;

                // Allocate an array to hold the names
                std::string *names = new std::string[length];

                // Ask user to enter all the names
                for (int i = 0; i < length; ++i)
                {
                    std::cout << "Enter name #" << i + 1 << ": ";
                    std::getline(std::cin , names[i]);
                }

                // Sort the array
                sortArray(names, length);

                std::cout << "\nHere is your sorted list:\n";
                // Print the sorted array
                for (int i = 0; i < length; ++i)
                    std::cout << "Name #" << i + 1 << ": " << names[i] << ‘\n’;

                delete[] names; // don’t forget to use array delete
                    names = nullptr; // use 0 if not C++11

                return 0;
            }

            • Alex

              Oh. You need to make sure you ignore the ‘\n’ in the stream, like this:

              Otherwise the first call to std::getline() will read the \n and treat it as an empty string input.

  • Kushagra

    Instead of this answer of quiz , can’t we rearrange names just similar to arranging integers like this

    #include <iostream>
    #include <algorithm>
    #include <string>
    int main()
    {
        std::cout << " How many names do you want to write ?" << std::endl;
        
        int length ;
        std::cin>>length;
        std::string *block = new std::string[length];
        
        for (int a = 0; a<length; a++)
        {
            std::cout << " name " << (a+1) << " is : ";
            std::cin >> block[a];
        }
        for (int g = 0; g<length; g++)
        {
        for ( int x =0; x<(length-1); x++)
        {
            if (block[x] >block[x+1])
            std::swap(block[x] , block[x+1]);
        }

        
        }
        
        std::cout << " The sorted order is " <<std::endl;
        
            for (int y = 0; y<length; y++)
        {
        std::cout << "name  " << (y+1) << " is :   " << block[y] <<std::endl;
        
        }
        delete[] block;
        block = nullptr;
        return 0;
    }

  • Frodo

    How can std::cin >> names[i] be altered so that std::cin.getline() is used?

  • 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