Похожие чаты

Guys Is it necessary to assign a nullptr to a pointer

(that points to an object that's allocated on the heap) after deleting that ?

Like:

delete ptr:
ptr = nullptr; // if necessary?

13 ответов

8 просмотров

generally yeah

Not necessary but doing so will help prevent Undefined Behavior and also help you notice it when you dereference the pointer by mistake.

Sahand-🏔️ Автор вопроса

Ok

No, it is not necessary, if the ptr is not going to be deleted again. Assigning nullptr is a safeguard against double deletion, but that is only in the case the ptr could be deleted again, for whatever misstep caused that to happen. Be particularly wary if that delete occurs in the middle of scope, and not at the end of scope. Deleting a nullptr is a no-op, and a waste of CPU cycles if unnecessary. A simple delete ptr; especially at end of scope in a destructor, is enough.

No, it is not necessary, if the ptr is not going to be deleted or referenced again. Assigning nullptr is mainly a safeguard against double deletion, but that is only in the case the ptr could be deleted again, for whatever misstep caused that to happen. Be particularly wary if that delete occurs in the middle of scope, and not at the end of scope. Note that deleting a nullptr is a no-op, and a waste of CPU cycles if unnecessary. Therefore, assigning nullptr to a ptr already deleted yet going out of scope is a waste. As another response pointed out, it could also help warn about dereferencing a deleted ptr, but that is the middle of scope issue I highlighted above. A simple delete ptr; especially at end of scope in a destructor, is enough.

Sahand 🏔️
Ok

If you use a smart pointer, you wouldn't have to worry about such things

its copied from bard

Kenshin Himura
What was copied from bard?

https://t.me/thedevs_c/249112

Malware (\/ /\ R |_| |\|)
https://t.me/thedevs_c/249112

You mean the response was copied from bard?

Malware (\/ /\ R |_| |\|)
yep

No, it was not. That response was typed directly from my knowledge of C++, having been familiar with the language since 2002. Use any language long enough, consult the proven experts, read articles and books, watch videos on various topics concerning the language, and keep in step with every new version, and responding with such a response is simple; it is simply one's experience shared for others to benefit from. I am flattered to be compared to an AI bot though.

Похожие вопросы

Обсуждают сегодня

А кто-то пробовал, уезжая из Эстонии получить э-рез и продолжить вести предпринимательскую деятельность внутри Эстонии, используя свой OÜ?
Lalalashechki Lalala
62
At which price point will BCH, an ASIC mined coin, be succeptible to reorg attacks because of low hashrate? $10? $1? $0.1? $0.01?
Kishniev
18
Is Kadena onto something special? No one really knows. Good luck!
Bright Uncle Stephen EqualBet
40
зачем же переименовывать ? чтобы кол-во участников возросло или вдруг IBM от этого снова на свифте начнет кодить ? Я не понимаю что страшного в том что свифт гавно, если это т...
Oleh Nerzh
10
Гайз, кто-нибудь пробовал запустить probe-rs под камень, которого нет в probe-rs? Мб есть какой-нибудь пример у кого... Через target-gen попробовал сгенерировать chip-descript...
Максим Смирнов
2
я не магистр хаскеля, но разве не может лейзи тип конвертнуться в не-лейзи запросив вычисление содержимого прям при инициализации?
deadgnom32 λ madao
100
здравствуйте. совершаю вот такую вещь: strcpy(line, (char)current_number); где current number — неподписанный шорт, line — массив чаров. ругань следующая: main.c:29:30: error...
Roberto's Ширгозиев
13
Hello VIC team, I had previously used the “wallet.tomochain.com” with a ledger connection. Unfortunately, the site is no longer accessible and the ledger account is not visibl...
carpe noctem
7
@MrMiscipitlick А можешь макрос написать, который будет вычислять смещение относительно переданных меток? Просто .label1-.label2, и вернуть значение.
КТ315
35
А еще в перле можно уже @arr1 + @arr2?
Sergei Zhmylove
53
Карта сайта