Weird behaviour with class fields when adding to a std::vector

Qq0 :

I have found some very weird behaviour (on clang and GCC) in the following situation. I have a vector, nodes, with one element, an instance of class Node. I then call a function on nodes[0] that adds a new Node to the vector. When the new Node is added, the calling object's fields are reset! However, they seem to return to normal again once the function has finished.

I believe this is a minimal reproducible example:

#include <iostream>
#include <vector>

using namespace std;

struct Node;
vector<Node> nodes;

struct Node{
    int X;
    void set(){
        X = 3;
        cout << "Before, X = " << X << endl;
        nodes.push_back(Node());
        cout << "After, X = " << X << endl;
    }
};

int main() {
    nodes = vector<Node>();
    nodes.push_back(Node());

    nodes[0].set();
    cout << "Finally, X = " << nodes[0].X << endl;
}

Which outputs

Before, X = 3
After, X = 0
Finally, X = 3

Though you would expect X to remain unchanged by the process.

Other things I have tried:

  • If I remove the line that adds a Node inside set(), then it outputs X = 3 every time.
  • If I create a new Node and call it on that (Node p = nodes[0]) then the output is 3, 3, 3
  • If I create a reference Node and call it on that (Node &p = nodes[0]) then the output is 3, 0, 0 (perhaps this one is because the reference is lost when the vector resizes?)

Is this undefined behaviour for some reason? Why?

NathanOliver :

Your code has undefined behavior. In

void set(){
    X = 3;
    cout << "Before, X = " << X << endl;
    nodes.push_back(Node());
    cout << "After, X = " << X << endl;
}

The access to X is really this->X and this is a pointer to the member of the vector. When you do nodes.push_back(Node()); you add a new element to the vector and that process reallocates, which invalidates all iterators, pointers and references to elements in the vector. That means

cout << "After, X = " << X << endl;

is using a this that is no longer valid.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=29253&siteId=1