c++ delete pointer issue, can still access data [closed]

Deleting a pointer doesn’t zero out any memory because to do so would take CPU cycles and that’s not what C++ is about. What you have there is a dangling pointer, and potentially a subtle error. Code like this can sometimes work for years only to crash at some point in the future when some minor change is made somewhere else in the program.

This is a good reason why you should NULL out pointers when you’ve deleted the memory they point to, that way you’ll get an immediate error if you try to dereference the pointer. It’s also sometimes a good idea to clear the memory pointed to using a function like memset(). This is particularly true if the memory pointed to contains something confidential (e.g. a plaintext password) which you don’t want other, possibly user facing, parts of your program from having access to.

Leave a Comment