为什么在循环中创建的对象具有相同的地址? [英] Why do the objects created in a loop have the same address?

查看:124
本文介绍了为什么在循环中创建的对象具有相同的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实看到了其他一些看起来像我的问题,但我仍然无法弄清楚.

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

这是我的代码:

#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";
    }
}

运行此命令时,新节点地址a"始终是每次迭代重复的相同地址,但是新节点地址b"每次都是不同的地址.

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

我不明白为什么a和b不一样.更具体地说:& newNode不是我刚刚创建的对象的地址吗?而且vectorOfNodes [i]也不是那个对象吗?那么& vectorOfNodes [i]也是对象的地址吗?

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?

推荐答案

在每门有关操作系统设计和/或编译器设计的大学课程中,都会介绍这些内容.

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...

现在,另一个对象在堆上分配.将您的注意力集中在以下事实上:在循环结束时,填充到向量 still 中的对象存在,而"a"对象被破坏,您将开始看到其中的区别.在循环的每次迭代中,都会创建"a"对象,然后在循环结束时销毁它(然后在循环的开头再次创建),而填充到向量中的对象仍然存在.

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.

这篇关于为什么在循环中创建的对象具有相同的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆