C++ delete – It deletes my objects but I can still access the data?

Is being able to access data from beyond the grave expected?

In most cases, yes. Calling delete doesn’t zero the memory.

Note that the behavior is not defined. Using certain compilers, the memory may be zeroed. When you call delete, what happens is that the memory is marked as available, so the next time someone does new, the memory may be used.

If you think about it, it’s logical – when you tell the compiler that you are no longer interested in the memory (using delete), why should the computer spend time on zeroing it.

Leave a Comment