Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Pointer Cleaner | Dynamic Memory
C++ Intermediate | Mobile-Friendly
course content

Kursinnehåll

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

1. Data Types and Arrays
2. References & Pointers
3. Dynamic Memory
4. Functions

book
Pointer Cleaner

The operator delete frees up the allocated memory, but it doesn’t delete the pointer:

12345678910
int *x = new int; *x = 42; cout << "Address: " << x << endl; cout << "Value: " << *x << endl; delete x; cout << "Address: " << x << endl; cout << "Value: " << *x << endl;
copy

As you can see, the pointer still remembers the address it refers to. Pointers that point to nonexistent units of memory are called dangling pointers. To avoid problems, you can assign to your pointer nullptr:

python

The keyword nullptr represents the value of the empty pointer. It’s a good practice to assign nullptr to a pointer so as not to crash your program by pointing to the unit of memory you will not use.

question mark

Which of the following statements is true?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 5

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

course content

Kursinnehåll

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

1. Data Types and Arrays
2. References & Pointers
3. Dynamic Memory
4. Functions

book
Pointer Cleaner

The operator delete frees up the allocated memory, but it doesn’t delete the pointer:

12345678910
int *x = new int; *x = 42; cout << "Address: " << x << endl; cout << "Value: " << *x << endl; delete x; cout << "Address: " << x << endl; cout << "Value: " << *x << endl;
copy

As you can see, the pointer still remembers the address it refers to. Pointers that point to nonexistent units of memory are called dangling pointers. To avoid problems, you can assign to your pointer nullptr:

python

The keyword nullptr represents the value of the empty pointer. It’s a good practice to assign nullptr to a pointer so as not to crash your program by pointing to the unit of memory you will not use.

question mark

Which of the following statements is true?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 5
Vi beklagar att något gick fel. Vad hände?
some-alt