c++ – Pointers, delete and nullptr

Question:

Glanders! Explain in as much detail as possible, please, what exactly delete and nullptr do for pointers. Here is the code:

````c++
#include <bits/stdc++.h>
using namespace std;

int main(){
 int a = 5;
 int *ptr = &a;
 int *ptr_ptr = ptr;
 cout << *ptr << endl;
 cout << *ptr_ptr << endl << endl;
 delete ptr; // 1 случай
 cout << *ptr_ptr << endl;
 cout << *ptr << endl << endl;
 ptr = nullptr; // 2 случай
 cout << *ptr_ptr << endl;
 cout << *ptr << endl;
}

Actually, the questions:

1) Why is 5 displayed in case 1, even though the ptr pointer has been deleted?

2) Why, in case 2, does not display 5 and the program crashes when trying to display ptr?

3) Why, in case 2, the output of ptr_ptr is successful, despite the fact that this pointer refers to ptr, when trying to output which the program crashes?

Answer:

Deleted – this means that this place is marked as free for other use, but not overwritten (this five continues to lie there), and the variable itself, in which the address is stored, does not change its value. So you are referring to the memory in which your top five lies, but which they can easily rewrite …

Just what you are doing is WRONG! : you cannot delete memory not allocated via new ! Now if you had

int * ptr = new int(5);

then it would be possible to write delete ptr

And in case 2, you change the value stored in the variable, and it does not point anywhere (that is, such a value – nullptr – cannot be a valid address for anything). And in ptr_ptr old pointer value remains – pointing to the very five (and not to ptr , as you write).

And in general – remember one rule – you freed up memory – that's it, forget about it. And if something works, then not every pedestrian crossing the road at a red light is knocked down … But this does not mean that he will not be knocked down next time.

I'll try an analogy. At home on the street – a memory, an envelope with an address – a pointer.

int * p = new int(5);

A lease agreement was signed, a piece of paper with the address of the house was enclosed in envelope p a five entered the house.

int *p_p = p;

Envelope p_p a copy of a piece of paper from envelope p .

cout << *p << endl;

cout looks into envelope p , goes to the house whose address is indicated there, looks who lives there, displays …

delete p; // 1 случай

The lease was terminated, but the five did not go anywhere. The address of the house continues to be in the envelope p – only this piece of paper is worthless: at any moment the house can be rented out to someone else and the new tenant will simply expose the old one …

cout << *p_p << endl;

cout looks into the p_p envelope, there is a copy of a piece of paper that has not gone anywhere. He goes to the house whose address is indicated there, looks who lives there, displays …

p = nullptr; // 2 случай

A piece of paper is thrown out of envelope p (replace it with a blank piece of paper without an address).

cout << *p_p << endl;

The envelope p_p still contains a piece of paper with the old address – nobody touched it … cout looks into the envelope p_p , goes to the house whose address is indicated there, looks who lives there, displays …

cout << *p << endl;

cout looks into envelope p , finds an empty piece of paper and does not know what to do …

Scroll to Top