2
class A{};

void Foo(A *p)
{ 
 std::cout << &p << std::endl; 
 std::cout << p << std::endl; 
}

int main()
{
 A *p = new A();
 std::cout << &p << std::endl;
 std::cout << p << std::endl;
 Foo(p);
}

The above program prints the same value for p but different addresses for &p. Can somebody please explain why ?

Dean Seo
  • 5,486
  • 3
  • 30
  • 49
jatniel
  • 47
  • 2
  • What does `&p` *mean*? *What* is it getting the "address of" if `p` (a A*) *already* represents a pointer to the created object? It may be useful to consider the *type* of the result of `&p`. – user2864740 Jan 02 '18 at 04:34
  • 1
    ``&p`` is the stack-based address of ``p`` – Asesh Jan 02 '18 at 04:35
  • I'm new to C++. What is stack-based address ?. Is the "&" given relative to the current stack of the method ? – jatniel Jan 02 '18 at 04:41
  • Possible duplicate of [What and where are the stack and heap?](https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) – Silvio Mayolo Jan 02 '18 at 05:23
  • man, address and address of address are totally different... – Yves Jan 02 '18 at 07:01
  • You could change the function to `void Foo(A *qq)` to make this less mysterious. – Bo Persson Jan 02 '18 at 09:43
  • There's an important difference between *what* a thing is, and *where* that thing is. Humans in general learn this at a very young age but mysteriously forget about it when they first hear the word "pointer". Also, giving things the same name does not make them the same thing. – molbdnilo Jan 02 '18 at 09:47

5 Answers5

6

The above program prints the same value for "p"

This is because one p is a copy of the other, so they both have the same value. The value of a pointer is a memory address where an object is stored so having the same value means pointing to the same object.

A function argument is a copy of the object that was passed to the function .

but different addresses for "&p". Can somebody please explain why ?

Each p here is a separate variable and a separate object ††. Both objects exist simultaneously. The C++ standard specifies that each currently existing object has a unique address †††, so therefore each p here must have a unique address.

The Unary operator & is the addressof operator, and it returns the memory address where the operand is stored.


Unless that function argument is a reference. In that case the reference is bound to the passed object. The p argument is not a reference.

†† Pointers themselves are objects. The memory address where a pointer stored is separate from the memory address that is its value which is the memory address of the pointed object.

††† There are exceptions in case of sub-objects, but those exceptions aren't relevant here.

eerorika
  • 232,697
  • 12
  • 197
  • 326
3
void Foo(A *p)
{ 
 std::cout << &p << std::endl; 
 std::cout << p << std::endl; 
}

When you pass something to Foo() that something is copied into p.So the actual parameter(the something which was passed) is not the same thing as the formal parameter(p here), though they will hold the same value. Inside Foo(), &p will print address of this formal parameter and not the address of the actual parameter that was passed.

And since the formal and actual parameter hold same value, p prints the same value.

Gaurav Sehgal
  • 7,422
  • 2
  • 18
  • 34
  • I see. Thanks. So the class A is not actually copied here right. ? . It is just printing the formal address instead of the actual address ? – jatniel Jan 02 '18 at 05:23
  • 1
    @jatniel The pointer to the class object of `A` is just copied into formal parameter `p`. – Gaurav Sehgal Jan 02 '18 at 05:31
1

operator & is returns Address of Variable

They are difference variable A *a and Foo(A *a) but they pointing to the same address. It's normally has difference address.

Bear0x3f
  • 142
  • 1
  • 16
1

Here is a good description of stack and heap. What and where are the stack and heap?

A short answer as others have mentioned is that:

p points to the allocated instance of A which is allocated from the heap. It is created with the operator 'new' in your code.

&p is pointing to the memory which p itself occupies. Just as class A occupies memory (which is allocated from the heap using the 'new' operator) p occupies memory (allocated from the stack since it is a local variable).

natersoz
  • 1,674
  • 2
  • 19
  • 29
1

this event occures because of copy constructors.

The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to −

-Initialize one object from another of the same type.

-Copy an object to pass it as an argument to a function.(this refer to your problem)

-Copy an object to return it from a function.

p is local variable that contain address of object in heap . and &p is address of p in stack. when we pass p to Foo() because of copy constructor compiler copy p to new local variable hence we have 2 pointer that both of them refer to same location in heap memory. on of them is original p(actual parameter) and second is unnamed local variable(Formal Parameter) in Foo() method that has been built by copy constructor.

also you could see difference between p an &p in below image.

p vs &p

hamed
  • 471
  • 1
  • 9
  • 22