Pointer Cleaner
The operator delete
frees up the allocated memory, but it doesn’t delete the pointer:
12345678910int *x = new int; *x = 42; cout << "Address: " << x << endl; cout << "Value: " << *x << endl; delete x; cout << "Address: " << x << endl; cout << "Value: " << *x << endl;
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
:
x = nullptr;
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.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Запитайте мені питання про цей предмет
Сумаризуйте цей розділ
Покажіть реальні приклади
Awesome!
Completion rate improved to 2.94
Pointer Cleaner
Свайпніть щоб показати меню
The operator delete
frees up the allocated memory, but it doesn’t delete the pointer:
12345678910int *x = new int; *x = 42; cout << "Address: " << x << endl; cout << "Value: " << *x << endl; delete x; cout << "Address: " << x << endl; cout << "Value: " << *x << endl;
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
:
x = nullptr;
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.
Дякуємо за ваш відгук!