每次获取不同的地址 [英] Getting different address every time

查看:113
本文介绍了每次获取不同的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我每次为 std :: vector v 的第一个元素获取不同的地址。为什么会这样?

In the following code, I get a different address every time for the first element of std::vector v. Why is it so?

#include <memory>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v;

    for (int i=0; i<10; ++i)
    {
        int b = i;

        v.push_back(b);

        std::cout << std::addressof(v[0]) << std::endl;
    }

    return 0;
}

输出

0x603010
0x603030
0x603010
0x603010
0x603050
0x603050
0x603050
0x603050
0x603080
0x603080


推荐答案

因为当调用

 v.push_back(b);

PS

您说:


在下面的代码中,我每次为std :: vector v的第一个元素获得一个不同的地址。为什么会这样?

In the following code, I get a different address every time for the first element of std::vector v. Why is it so?

如果你看看你的输出,那并不是每次都是这样:)

If you look at your output, that is not true for every time :)

这篇关于每次获取不同的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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