(that points to an object that's allocated on the heap) after deleting that ?
Like:
delete ptr:
ptr = nullptr; // if necessary?
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.
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.
If you use a smart pointer, you wouldn't have to worry about such things
i am very sure that its generated by bard
its copied from bard
What was copied from bard?
https://t.me/thedevs_c/249112
You mean the response was copied from bard?
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.
Обсуждают сегодня