C++ pointer address issue

雨燕双飞 提交于 2019-12-02 23:52:55

问题


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

i get this output:

0031FB2B

0057C200

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?


回答1:


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

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.



来源:https://stackoverflow.com/questions/23435339/c-pointer-address-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!