Today it has become clear through a series of SO questions that I have a poor understanding of the true nature of pointers, references, and values.
Consider the following code:
int* p = new int(3); int& r = *p; cout << " p = " << p << "\t*p = " << *p << endl; cout << "&r = " << &r << "\tr = " << r << endl; delete p; cout << "&r = " << &r << "\tr = " << r << endl; int v = 4; r = v; cout << "&r = " << &r << "\tr = " << r << endl;
Way out for that
p = 0x1001000b0 *p = 3 &r = 0x1001000b0 r = 3 &r = 0x1001000b0 r = 3 &r = 0x1001000b0 r = 4
I do not understand why the second time I print the value of the link, that I am not getting an error. The pointer corresponding to the value of the link has already been deleted. From the previous previous question, I almost convinced myself that any statement like r = x makes a copy of x instead of the value that r refers to. However, if so, then p and &r will be different addresses, right? If I already called delete on 0x100100b0, then how can I continue to use it?
True or False: A link is the same as an alias for a value at an address.
True or false: if you delete the pointer to the same address as the reference value (as I do above), the undefined behavior will not be executed, and no one will ever rewrite this address as if the link exists.
c ++ pointers reference
Jnbrymn
source share