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?

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top