skip to content

In C++, what happens when a range-based for loop over a std::vector calls push_back on that same vector?

level: middleimportance: must knowfreq 78%

answer

  1. the loop caches two iterators
  2. capacity decides the damage
  3. past-the-end always invalidated
  4. reallocation frees the old buffer

basics

~10 s

It is undefined behaviour. The loop holds cached begin and end iterators; push_back always invalidates the end iterator, and when size exceeds capacity it reallocates, invalidating every iterator and reference, including the loop variable.

solid answer

~40 s

A range-based `for` is shorthand for a loop over two iterators fetched once: `auto begin = v.begin(); auto end = v.end(); for (; begin != end; ++begin)`. `std::vector::push_back` has two outcomes. If the new size exceeds `capacity()`, it reallocates: elements move to a new buffer, the old one is freed, and every iterator, pointer and reference into the vector is invalidated, including the loop variable, so the next read is a use-after-free. If there is room, only iterators at or after the insertion point are invalidated, which for `push_back` is the past-the-end iterator the loop compares against. Either way the loop is undefined behaviour, and `reserve` does not rescue it. Fix it with an index loop that re-reads `size()`, or collect the new elements in a separate vector and append them after the loop.

code

cpp · 16 lines
cpp
#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> playlist{"Intro", "Verse", "Chorus"};
    std::cout << "capacity before: " << playlist.capacity() << '\n';

    // Bug: push_back inside a range-for over the same vector.
    for (const std::string& song : playlist) {
        if (song == "Chorus") {
            playlist.push_back("Chorus (reprise)");  // may reallocate
        }
        std::cout << song << '\n';  // reads through a possibly dangling reference
    }
}

go deeper

for a junior

Recall that a range-for over a vector must not add to or erase from that vector, and that adding can move every element to new memory.

for a middle

Explain the desugared loop with its cached begin and end, then the two push_back cases from the standard: reallocation invalidates everything, otherwise past-the-end and later positions.

for a senior

Diagnose the release-only crash or skipped element from a sanitizer report, explain why capacity made it intermittent, and pick a fix that fits: index loop, staging vector, or a different container.

for a principal

Decide whether a team bans container mutation inside range-for outright in review guidelines and runs AddressSanitizer in CI, since these bugs depend on capacity and slip past ordinary tests.

## What a range-based for loop really is A **range-based `for`** statement is defined by the standard as a rewrite into an ordinary loop. For `for (const std::string& song : playlist) body`, the compiler behaves as if you had written: ```cpp { auto&& range = playlist; auto begin = range.begin(); auto end = range.end(); for (; begin != end; ++begin) { const std::string& song = *begin; body } } ``` The names are exposition only, but the shape matters: `begin()` and `end()` are called **once**, before the first iteration, and the loop keeps comparing against that cached `end`. Nothing re-reads the vector's size. The loop variable `song` is a reference into the vector's own buffer. ## What push_back does to iterators The standard's rule for inserting into a `std::vector` has two branches, decided by `capacity()`, the number of elements the current buffer can hold: 1. **The new size exceeds the capacity.** The vector **reallocates**: it allocates a larger buffer, moves or copies the elements across, and frees the old buffer. Reallocation invalidates *all* references, pointers and iterators into the vector, as well as the past-the-end iterator. The loop's `begin`, its cached `end` and the reference `song` now all point into freed memory. 2. **There is spare capacity.** No element moves. Iterators and references *before* the insertion point stay valid, while those at or after it, including the past-the-end iterator, are invalidated. For `push_back` the insertion point is the end, so the casualty is exactly the `end` iterator the loop compares against. Either way, the loop then uses an invalidated iterator, which is **undefined behaviour**. The program might crash, silently read garbage, skip the new element or appear to work; none of those outcomes is the answer. ## Why it often survives testing Whether a given `push_back` reallocates depends on the current capacity, and the growth policy is an implementation choice. The standard requires only amortized constant-time `push_back`; libc++ and libstdc++ both grow to roughly twice the old capacity. So a loop that pushes once into a vector built with spare room never reallocates in a test, then reallocates in production when the input size lands exactly on a capacity boundary. With libc++, a vector built from a three-element initializer list has capacity 3, and the first `push_back` reallocates; built with `-fsanitize=address`, the example stops with `heap-use-after-free` at the line that prints `song`. A sanitizer run is the fastest diagnosis. Without one, symptoms range from corrupted strings to crashes far from the loop. ## Fixes that are actually correct - **Index the vector.** `for (std::size_t i = 0; i < v.size(); ++i)` re-reads `size()` on every pass, and `v[i]` re-reads the current buffer on every access. Do not hold a reference such as `auto& s = v[i]` across the `push_back`, because it dangles after a reallocation. - **Stage the additions.** Collect new elements into a separate vector during the loop, then append them afterwards with `v.insert(v.end(), extra.begin(), extra.end())`, or with `v.append_range(extra)` in C++23. This also makes it obvious whether new elements should themselves be visited. - **Choose a container whose rules allow it.** `std::list::push_back` invalidates no iterators, so the range-for is well-defined there and will visit appended elements; the loop then needs its own stopping condition. What is **not** a fix: - `v.reserve(...)` before the loop. It prevents reallocation, so the elements and `song` stay valid, but `push_back` still invalidates the cached past-the-end iterator. The loop remains undefined; it merely stops crashing. - Making the loop variable `const`. That stops writes through `song`, not changes to the container. ## The same question for other containers | container | `push_back` inside a range-for over it | |---|---| | `std::vector` | undefined: reallocation invalidates everything, otherwise past-the-end | | `std::deque` | undefined: insertion at an end invalidates all iterators, though references survive | | `std::list` | well-defined: no iterator or reference is invalidated; new elements are visited | ## Summary for the interview State that the loop is undefined behaviour, then show why: the rewrite caches `begin` and `end`, and `push_back` either reallocates (everything invalid) or invalidates past-the-end (the loop's bound). Mention that capacity makes the bug intermittent, that `reserve` does not fix it, and give the index-loop or staging-vector repair.

  • Is the same loop safe over a std::list instead of a std::vector?
    Yes. `std::list::push_back` does not affect the validity of any iterator or reference, including `end()`, so the loop is well-defined, and it will visit the newly appended elements too. That makes it a termination question rather than a safety one: append unconditionally and the loop never ends.
  • Does calling v.reserve(v.size() + 10) before the loop make it correct?
    No. Reserving prevents reallocation, so the elements and the loop variable stay valid, but `push_back` still invalidates the past-the-end iterator the loop cached, and comparing against an invalidated iterator is undefined behaviour. It only turns a crash into a quieter bug.
  • Which standard wording decides what push_back invalidates?
    The vector modifiers clause: inserting causes reallocation when the new size is greater than the old capacity, and reallocation invalidates all references, pointers and iterators, plus the past-the-end iterator. Without reallocation, those before the insertion point stay valid and those at or after it, including past-the-end, are invalidated.

  • Says the loop simply sees the new elements at the end
  • Believes reserve() beforehand makes the range-for loop well-defined
  • Thinks only the new element's iterator is affected by push_back
  • Assumes it is safe because it never crashed in testing
  • Claims the standard requires vector capacity to double