Upgrade Upgrade - 22 days ago 5
C++ Question

Address of a pointer vs Reference to a pointer

I wrote this code:

int* p = new int(7);
std::cout << p << std::endl; //output: 0096FAB4
std::cout << &p << std::endl; //output: 0096FA90


Why is the output diferent diferent?

Answer

Each object in C++ is represented by the address of the physical location of this object. If you create an address type int *p = new int(7); so when you call std::cout << p << std::endl; it will output the address of the cell in memory, where the address of number 7 is located. And when you call std::cout << &p << std::endl; so it will output the physical address of cell in memory, where the number 7 is located.

To conclude:

  • //output: 0096FAB4 is the address of address of the integer 7
  • //output: 0096FA90 is the physical address of the integer 7

Example:

  • If we look in the memory by the address 0096FAB4 we will see 0096FA90
  • If we look in the memory by the address 0096FA90 we will see 00000007