Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

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

I do see some other questions that look like mine, but I still can't figure this out.

Here's 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 << "
";
        vectorOfNodes.push_back(newNode);
        std::cout << "new node address b "<<    &vectorOfNodes[i] << "
";
    }
    for (int i = 0; i< 4; i++){ 
        std::cout << "data "<<    vectorOfNodes[i].data << "
";
    }
}

When I run this, "new node address a " is always the same address repeated each iteration but "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 don't understand why a and b aren't the same. More specifically: isn't &newNode the address of the object I just created? And isn't vectorOfNodes[i] also that object? So isn't &vectorOfNodes[i] also the address of the object?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

The objects in question are instantiated on the stack, and at the end of the scope, they go out of scope on the stack. The stack grows and shrinks accordingly, and because on every iteration of the loop, the same exact objects get created (and destroyed), the stack grows and shrinks by the same amount, so the same objects always end up getting instantiated at the same location on the stack.

In practice, the compiler can do some optimizations and figure out the large amount of stack each function will use, most of the time, and thus "pregrow" the stack to the large amount the function will need; but that's going a bit too far into the weeds...

This is as concise answer as I can give, without going into an entire lecture of what a stack is, etc...

Now, the other object is allocated on the heap. Focus your attention on the fact that, at the end of the loop, the object you stuffed into the vector still exists, while the "a" object gets destroyed, and you'll begin to see the difference. On each iteration of the loop the "a" object gets created, and then destroyed at the end of the loop (and get created at the beginning of the loop again), while the object you stuffed into the vector still exists.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...