Understanding links and pointers. Why does it work?

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.

+6
c ++ pointers reference
source share
7 answers

Even if you do not receive an error message, the results are still undefined. Undefined behavior means that anything can happen, including your program, which continues to work correctly.

A link is the same as an alias for a value at an address.

It really is. It would be more correct to say that the link is an alias for the object (and not the value).

If you delete the pointer to the same address as the reference value (as I already did above), the Undefined behavior will not be executed,

This is also true. There is no specific behavior until you try to use the link. When you try to use a link (e.g. via &r ), you get Undefined behavior.

If you never try to use a link after destroying an object, there is no Undefined behavior.

no one will rewrite this address as long as the link exists.

No, this is not true. As soon as the object is destroyed, any links or pointers to it are invalid and unusable. If you try to use a pointer or a reference to the destroyed object, the results will be undefined.

+11
source share

The link is an alias for the object. If this lifetime of an object has ended, the reference to this object ceases to be valid.

In your example, using r referencing after the delete p operation leads to undefined behavior - the fact that it “appears” to work is just a coincidence - using r after this point is invalid as using *p after this point (and will look like to the same behavior.

For example, changing your program to perform the same ticks with *p , like with r , looks like this:

 #include <iostream> using namespace std; int main() { int* p = new int(3); int& r = *p; cout << " p = " << p << "\t*p = " << *p << endl; cout << "&r = " << &r << "\tr = " << r << endl; delete p; cout << " p = " << p << "\t*p = " << *p << endl; cout << "&r = " << &r << "\tr = " << r << endl; int v = 4; *p = v+1; cout << " p = " << p << "\t*p = " << *p << endl; r = v; cout << "&r = " << &r << "\tr = " << r << endl; } 

Output:

  p = 0x3f1730 *p = 3 &r = 0x3f1730 r = 3 p = 0x3f1730 *p = 4134736 &r = 0x3f1730 r = 4134736 p = 0x3f1730 *p = 5 &r = 0x3f1730 r = 4 

You will see a similar behavior when using *p after deleting an object (and this is not the case).

In all cases, access to an object after its deletion is undefined behavior, whether this access occurs through an invalid pointer or invalid link.

+1
source share

True or false: the link is the same as an alias to the value of the address.

Errr ... true? Your terminology makes this unclear, but I think I got what you ask.

True or false: if you delete the pointer to the same address as the link value (like me above), then no undefined will happen, and no one will ever rewrite this address if the link exists.

False You lose the correct value after you allocated unallocated memory just because your C ++ implementation is not paranoid in memory management, and you access it immediately after its expiration.

You see, during compilation, the compiler has no way to predict that you are going to pull out this dirty trick, and microprocessor memory management will be expensive during operation.

So why is this a bad practice and what could go wrong? 1) if between your “delete p” and 3rd cout you perform large operations with large memory with a lot of “new” and “deleted” calls - there is a possibility that the actual memory point “r” refers to (its value is changed or completely inaccessible from -for freeing the operating system memory - which will cause a general protection error and application failure)

2) if you compile and run your application in some kind of paranoid (or resource) environment, you will get crashes because the system keeps track of which memory belongs to your application, even to the scale of the value "int".

If you want to get more detailed information, you can find the topics "C ++ heap" and "C ++ memory management".

+1
source share

The first is true:

  • The link is indeed an alias for the value. The specific implementation of the link does not change this fact and should not be relied on.

Second lie:

  • If you delete a pointer to the same address, the only final source of what happens if you use a previously created link is the language specification. I believe the case in C ++ is "undefined behavior". What happens in your example is that (I hypothesize here), the link is implemented as a pointer and therefore it still points to a memory block that has a value of 3 when interpreting int . This block was released in a heap for possible future use, so there is no guarantee that what it contains is actually 3 or even legal value for the reference type (although, of course, every possible value is legal for int - it could be class type though). In practice, since the value has not been overwritten by anything else, you still get a “meaningful” result when using the link. However, this happens by "coincidence."

Update: I made a mistake in answering the first question, reworked the answer.

0
source share

A reference is the same thing as an alias to the value at an address.

True

True or false: If you delete a pointer to the same address as a referenced value resides, (as I do above), then no undefined behavior will occur, and no one will ever overwrite that address as long as the reference exists.

False

 int v = 4; r = v; 

compiles because r is a reference to int.

However, when you delete all landmarks and then try to use it, this behavior is undefined!

0
source share

Your first question is too vague to answer. What is an alias for a value at an address?

The second is definitely false. The memory in which p was indicated was freed and can be reused. It just happens that he has not changed yet. It is undefined to use r , and r = v dereferenced r and the copied value of v there (which was also undefined - could be broken).

When something is undefined, it does not mean that you will receive an error message. This means that any behavior you get is considered a specification. Failure or operation is acceptable for "undefined" behavior.

0
source share

True or False: A link is the same as an alias for a value at an address.

False: it has nothing to do with the address (this is an implementation detail).

True or false: if you delete a pointer to the same address as the reference value (as I do above), then undefined behavior will not happen, and no one will ever rewrite this address if there is a link.

False The undefined behavior is used to refer to a variable that is no longer live. Different types of variables die at different times.

  • variables for automatic storage duration are stored at the end of the scope.
  • static storage parameters are stored in reverse order of creation after the main outputs
  • dynamic parameters of data storage duration die when delete is called in the memory in which they are located.
0
source share

All Articles