Reputation: 4771
in this code:
int* a;
int* b;
int c;
int* d;
a=new int(5);
b=&(*a);
c=*a;
d=&c;
cout<<"*a = "<<*a<<endl;
cout<<"a = "<<a<<endl;
cout<<"b ="<<b<<endl;
cout<<"d = "<<d<<endl;
I get:
*a = 5
a = 0x83a2008
b =0x83a2008
d = 0xbfbfe540
why is d different from b? Aren't they both &(*a) ? how can I get the d result with a single line?
Thanks a lot.
Upvotes: 3
Views: 205
Reputation: 490218
a
points to a dynamically allocated location, holding the value 5. When you do c = *a;
, you're copying the value 5 from that dynamically allocated location into c
. You're then taking the address of c
and assigning it to d
(and printing it out).
When you end up with is something like this:
The solid lines indicate a pointer referring to a location. The dashed line indicates movement of data.
Upvotes: 7
Reputation: 61920
I think there is something in C11/C99 Section 6.5.3.2 Paragraph 4 and footnote 102
a pointer to the object or function designated by its operand. The unary * operator denotes indirection. If the operand points to a function, the result is a function designator; if it points to an object, the result is an lvalue designating the object. If the operand has type ‘‘pointer to type’’, the result has type ‘‘type’’. If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined.102)
And footnote 102 tells
102) Thus, &*E is equivalent to E (even if E is a null pointer), and &(E1[E2]) to ((E1)+(E2)). It always true that if E is a function designator or an lvalue that is a valid operand of the unary operator, *&E is a function designator or an lvalue equal to E. If *P is an lvalue and T is the name of an object pointer type, *(T)P is an lvalue that has a type compatible with that to which T points. Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer, an address inappropriately aligned for the type of object pointed to, and the address of an object after the end of its lifetime. is &
EDIT
c = *a;
will result in an undefined behaviour, as the memory location 5 is not allocated.
Upvotes: 1
Reputation: 2887
c
is its own int
and not an int*
. When you say c = *a
, you're actually assigning the value pointed to by a
to a separate integer with its own space in memory (even though this int
is not an int*
, it still has to exist somewhere in memory).
So when you say d = &c
, you're getting the address of c
, not the value of a
.
Upvotes: 6
Reputation: 6371
c
is new instance of integer that's why d
is not equal a
nor b
. So when You try to get address of value stored in a
you get exactly address of a
. But when you first assign value of a
to c
and then print address of c
you get different address than a
. In C++ you operate on real objects not references like in Java or C#. when you assign value to variable you copy every filed stored in this variable, but be careful - by default =
operator don't do deep cloning.
Upvotes: 1
Reputation: 87426
I don't know all the rules and subtleties that go into this, but the compiler is able to simplify &(*a)
to just be a
. So you are initializing b
to point at the same integer that a
points at (b = a
). Then you are copying the integer by value to c
. Then you are initializing d
to point at c
. So a
and b
point to the same integer, but d
points to something else.
Upvotes: 0
Reputation:
d is the address of c which is a local stack variable. b is the address of the integer you allocated on the heap and stored the address of in a.
Upvotes: 1
Reputation: 363627
After
d = &c;
d
points to the storage location designated by c
, which is not the storage location pointed to by a
.
Upvotes: 1