Pergunta

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?

Foi útil?

Solução

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top