Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What would happen if I append elements in range based for?

I want to modify (push_back) element while iterate vector like this:

auto main() -> int {
    std::vector<double> v { 1, 2, 3 };

    for (auto& num : v) {
        std::cout << num << "\n";
        v.push_back(num);
    }

    std::cout << "===========\n";

    for (const auto& num : v) {
        std::cout << num << "\n";
    }
}

The output is:

1
1.00938e-320
0
===========
1
2
3
1
1.00938e-320
0

Why the output is not something like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1
2
3
===========
1
2
3
1
2
3

If range based for is just sugar of:

for (auto& it{v.begin()}, it != v.end(), it++)

Shouldn’t there be two possible situations here?

  1. v.end() evalued every iterate and never end. It should be an infinite loop.

  2. v.end() evalued one time before iterate and should output as I expected:

1
2
3
===========
1
2
3
1
2
3

>Solution :

As you can see when you call std::vector::push_back:

If after the operation the new size() is greater than old capacity() a
reallocation takes place, in which case all iterators (including the
end() iterator) and all references to the elements are invalidated.
Otherwise only the end() iterator is invalidated.

(emphasis is mine)

Range-based loop is only a syntactic sugar for using the iterators (and specifically compare to the end() iterator after each iteration).
Therefore calling push_back inside it invokes undefined-behavior.

This means the standard gives no guarantee for the output of the program. Any output you observe is legitimate.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading