I have the following code
// Example program
#include <iostream>
#include <string>
#include <vector>
struct Foo {
int value = 0;
};
int main()
{
std::vector<Foo> objs;
for (int idx = 0; idx < 5; idx++) {
objs.push_back(Foo{.value = idx});
std::cout << "address: " << &(objs.back()) << "n";
}
std::cout << "=======n";
for (const auto& obj: objs) {
std::cout << "address: " << &(obj)<< "n";
}
}
Now, I expect the object address are the same, but apparently they are not, see below for a screenshot.
How do I understand this? Are the objects moved/relocated internally by the std::vector class somehow?