0

Hi just a simple question, when I am clearing a vector of pointers, do I absolutely have to reaffect the deleted pointers to nullptr? If not what would ne the difference between doing so or just deleting them?

std::vector<object*> objects;
for(auto&& object : objects) {delete object; object = nullptr; } objects.clear()
  • No you don't, and it makes no difference whatsoever. What led you to believe that this is needed, what do you believe that would accomplish? – Sam Varshavchik Dec 05 '21 at 03:17
  • In this case, no, it makes no difference and it's not good practice in this case (nor is it bad practice, but it's a bit frowned up). Could be useful if you have some sort of memory bug with a dangling pointer to help diagnose the problem. – Eljay Dec 05 '21 at 03:21

0 Answers0