C ++指针地址问题 [英] C++ pointer address issue

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

问题描述

int *i = new int;
cout << &i << endl << i;
delete i;
i = 0;

我得到以下输出:

0031FB2B

0057C200

为什么有2个不同的地址?不是&引用动态指针的地址,而我本身就是指针的地址,应该是相同的地址吗?

Why 2 different addresses? Isn't & referencing the address of the dynamic pointer and i itself the address of the pointer, which should be the same address?

推荐答案

&i是指针的地址.在这里将存储new返回的值. i是指针本身的值,这是new返回的值.

&i is the address of the pointer. This is the place where the value returned by new will be stored. i is the value of the pointer itself, this is the value returned by new.

为了完整起见,* i是所指向的整数的值,该整数目前尚未初始化,但这是您实际数据所用的地方.

And just for completeness, *i is the value of the integer pointed to, which at the moment is uninitialized, but this is where your actual data will go.

这篇关于C ++指针地址问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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