C ++ delete - 它删除我的对象,但我仍然可以访问数据? [英] C++ delete - It deletes my objects but I can still access the data?

查看:143
本文介绍了C ++ delete - 它删除我的对象,但我仍然可以访问数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个简单的,工作的tetris游戏,每个块作为一个类单块的实例。

  class SingleBlock 
{
public:
SingleBlock(int,int);
〜SingleBlock();

int x;
int y;
SingleBlock * next;
};

class MultiBlock
{
public:
MultiBlock(int,int);

SingleBlock * c,* d,* e,* f;
};

SingleBlock :: SingleBlock(int a,int b)
{
x = a;
y = b;
}

SingleBlock ::〜SingleBlock()
{
x = 222;
}

MultiBlock :: MultiBlock(int a,int b)
{
c = new SingleBlock(a,b);
d = c-> next = new SingleBlock(a + 10,b);
e = d-> next = new SingleBlock(a + 20,b);
f = e-> next = new SingleBlock(a + 30,b);
}



我有一个功能,扫描一个完整的行,删除相关的块并重新分配 - >下一个指针的块列表。

  SingleBlock * deleteBlock; 
SingleBlock * tempBlock;

tempBlock = deleteBlock-> next;
delete deleteBlock;

游戏正常工作,块被正确删除,一切都按照原样运行。



如果我在每个删除的单个块x的值被删除后,其中一些返回随机垃圾(确认删除),其中一些返回222,告诉我,即使析构函数被调用的数据没有从堆中实际删除。



结果:

 现有块:
块:00E927A8
块:00E94290
块:00E942B0
块:00E942D0
块:00E942F0
块:00E94500
块:00E94520
块:00E94540
块:00E94560
块:00E945B0
块:00E945D0
块:00E945F0
块:00E94610
块:00E94660
块:00E94680
块:00E946A0

删除块:
正在删除... 00E942B0,X = 15288000
正在删除... 00E942D0,X = 15286960
正在删除... 00E94520,X = 15286992
正在删除... 00E94540,X = 15270296
正在删除... 00E94560,X = 222
正在删除... 00E945D0,X = 15270296
正在删除... 00E945F0,X = 222
正在删除... 00E94610,X = 222
正在删除... 00E94660,X = 15270296
正在删除... 00E94680,X = 222



很抱歉,如果这有点长。



$ b $

b

这在技术上称为未定义的行为。不要惊讶,如果它给你一罐啤酒。


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

class SingleBlock
{
    public:
    SingleBlock(int, int);
    ~SingleBlock();

    int x;
    int y;
    SingleBlock *next;
};

class MultiBlock
{
    public:
    MultiBlock(int, int);

    SingleBlock *c, *d, *e, *f;
};

SingleBlock::SingleBlock(int a, int b)
{
    x = a;
    y = b;
}

SingleBlock::~SingleBlock()
{
    x = 222;
}

MultiBlock::MultiBlock(int a, int b)
{
    c = new SingleBlock (a,b);
    d = c->next = new SingleBlock (a+10,b);
    e = d->next = new SingleBlock (a+20,b);
    f = e->next = new SingleBlock (a+30,b);
}

I have a function that scans for a complete line, and runs through the linked list of blocks deleting the relevant ones and reassigning the ->next pointers.

SingleBlock *deleteBlock;
SingleBlock *tempBlock;

tempBlock = deleteBlock->next;
delete deleteBlock;

The game works, blocks are deleted correctly and everything functions as it is supposed to. However on inspection I can still access random bits of deleted data.

If I printf each of the deleted singleblocks "x" values AFTER their deletion, some of them return random garbage (confirming the deletion) and some of them return 222, telling me even though the destructor was called the data wasn't actually deleted from the heap. Many identical trials show it is always the same specific blocks that are not deleted properly.

The results:

Existing Blocks:
Block: 00E927A8
Block: 00E94290
Block: 00E942B0
Block: 00E942D0
Block: 00E942F0
Block: 00E94500
Block: 00E94520
Block: 00E94540
Block: 00E94560
Block: 00E945B0
Block: 00E945D0
Block: 00E945F0
Block: 00E94610
Block: 00E94660
Block: 00E94680
Block: 00E946A0

Deleting Blocks:
Deleting ... 00E942B0, X = 15288000
Deleting ... 00E942D0, X = 15286960
Deleting ... 00E94520, X = 15286992
Deleting ... 00E94540, X = 15270296
Deleting ... 00E94560, X = 222
Deleting ... 00E945D0, X = 15270296
Deleting ... 00E945F0, X = 222
Deleting ... 00E94610, X = 222
Deleting ... 00E94660, X = 15270296
Deleting ... 00E94680, X = 222

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

Sorry if this is a bit long winded.

解决方案

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

This is technically known as Undefined Behavior. Don't be surprised if it offers you a can of beer either.

这篇关于C ++ delete - 它删除我的对象,但我仍然可以访问数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆