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

前端 未结 13 1855
礼貌的吻别
礼貌的吻别 2020-11-21 06:11

I have written a simple, working tetris game with each block as an instance of a class singleblock.

class SingleBlock
{
    public:
    SingleBlock(int, int)         


        
13条回答
  •  再見小時候
    2020-11-21 07:01

    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.

提交回复
热议问题