Why do objects created in a loop have the same address?

I see some other questions similar to mine, but I still can't figure it out.

Here is my code:

#include<iostream> #include <vector> using namespace std; template<typename Data_Type> class node { public: Data_Type data; node* next; node(Data_Type data, node* next){ this->data = data; this->next = next; } }; int main(){ vector<node<int> > vectorOfNodes; for (int i = 0; i< 4; i++){ node<int> newNode = node<int>(i,NULL); std::cout << "new node address a "<< &newNode << "\n"; vectorOfNodes.push_back(newNode); std::cout << "new node address b "<< &vectorOfNodes[i] << "\n"; } for (int i = 0; i< 4; i++){ std::cout << "data "<< vectorOfNodes[i].data << "\n"; } } 

When I run this, the β€œnew node address a” always matches the address repeating at each iteration, but the β€œnew node address b” is a different address each time.

 new node address a 0x7fff546c39b0 new node address b 0x7fe2f8c039d0 new node address a 0x7fff546c39b0 new node address b 0x7fe2f8c039f0 new node address a 0x7fff546c39b0 new node address b 0x7fe2f8c03a20 new node address a 0x7fff546c39b0 new node address b 0x7fe2f8c03a30 data 0 data 1 data 2 data 3 

I do not understand why a and b do not match. In particular: is not & newNode the address of the newly created object? And is not vectorOfNodes [i] also this object? So, is not "vectorOfNodes [i] also the address of an object?

+7
c ++ pointers
source share
3 answers

This material is explained in every college-level course on operating system design and / or compiler development.

The objects in question are created on the stack, and at the end of the scope they exit the scope on the stack. The stack grows and shrinks accordingly, and since the same exact objects are created (and destroyed) at each iteration of the loop, the stack grows and shrinks by the same amount, so the same objects always get an instance in the same place on the stack .

In practice, the compiler can do some optimizations and calculate the large stack size that each function will use most of the time, and thus drag the stack over the large amount that the function will need; but it goes too far into the weeds ...

This is the shortest answer that I can give without going into a whole lecture on what a stack is, etc.

Now another object is allocated on the heap. Focus on the fact that at the end of the loop, the object you nested in the vector still exists, and the object β€œa” is destroyed, and you will begin to see the difference. At each iteration of the loop, an β€œa” object is created and then destroyed at the end of the loop (and created again at the beginning of the loop), while the object that you insert into the vector still exists.

+9
source share

The push_back operation copies newNode to the vector. Therefore, each copy has a different address.

0
source share

Here

 node<int> newNode = node<int>(i,NULL); 

You are not creating a new node by simply assigning it a new value.

New nodes are created inside the vector using the default copy constructor. If you want your vector to contain the nodes that you insert into it, you should use a pointer, for example:

 vector<node<int>*> vectorOfNodes; 
-2
source share

All Articles