简体   繁体   中英

Why does same pointer has different addresses

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 ?

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.

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM