为什么在C ++中执行删除操作后仍可以访问释放的动态分配的内存? [英] Why can freed dynamically allocated memory still be accessed after a delete operation in C++?

查看:131
本文介绍了为什么在C ++中执行删除操作后仍可以访问释放的动态分配的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经分配了一些内存来存储这样的int值:

Suppose I have allocated some memory for storing an int value like this:

int *p=new int; 

在这里,我使用 new 创建了所需的内存操作符并分配了该内存块的地址,以便我可以访问该内存块。

Here I created the required memory using new operator and assigned the address of that memory block so that I can access that memory block.

现在,我可以控制存储在该内存块中的内容。

Now it's my control to what I store in that memory block.

但是当我写这样的语句时:

But when I write a statement like this:

delete p;

我们说我已经删除了动态分配的内存。

we say that I have deleted the dynamically allocated memory.

但是,如果我真的<删除了$ 删除后我是否应该不能再访问该内存? 操作?但是我能够使用相同的指针变量访问该内存块。那么,如果我们可以在删除内存块之后再访问该内存块,那么删除内存块的目的是什么?

But if I really delete'd or freed up that memory, should I not be able to access that memory anymore after the delete operation? But I was able to access that memory block using the same pointer variable. So what was the purpose of deleting the memory block if we can access the block after deleting it?

下面是一些示例代码:

#include <iostream>
using namespace std;

int main(void)
{
    int *p;
    p=new int;
    *p=10;
    cout << *p << endl;
    delete p;

    //here look we can still access that block of memory using the same pointer variable
    //But we did not created the  memory block after deletetion
    cout << *p << endl;
    *p=20;
    cout << *p << endl;

    return 0;
}

删除/释放动态分配的内存是什么意思

What does the saying "deleted/ free up the dynamically allocated memory" means in this context?

推荐答案

其他答案是说,在删除是未定义的行为是正确的。但是我认为了解一些情况是有用的,而不是简单地说任何事情都会发生。

The other answers saying that dereferencing a pointer after delete is undefined behavior are correct. But I think it's useful to get some understanding of what's going on instead of simply saying "anything can happen".

当您删除指针,程序将执行几个步骤:

When you delete a pointer, several steps are taken by the program:


  1. 调用指针所引用对象的析构函数

  2. 调用内存管理器的函数,该函数将进行簿记以释放内存。

第二个根据实现的不同,步骤实际上可能会将内存块返回给OS,也可能只是标记该块可用于后续分配,或者执行其他操作。在您的情况下,很明显,该块只是被标记为空闲,而没有返回给操作系统。进一步的分配可能会在该块内返回一个地址,然后未定义的行为可能会从正常工作变为正常工作。

The second step, depending on implementation, may actually return the memory block to the OS, or it may simply mark that the block is available for subsequent allocation, or do something else. In your case, apparently, the block is simply marked as free but not returned to the OS. A further allocation may return an address within this block, and then the undefined behavior will likely change from "working fine" to "working strange".

但是即使您仍然可以通过已删除的指针访问该对象,编译器仍可以假定该对象不再存在,并进行一些依赖于此的优化。这可能会以非常意外和看似不合逻辑的方式破坏您的程序,因此您最好远离未定义的行为。

But even if you still can access the object through the pointer you've deleted, the compiler can still assume that the object no longer exists and do some optimizations which rely on this. This may break your program in very unexpected and seemingly illogical ways, so you'd better keep away from undefined behavior.

这篇关于为什么在C ++中执行删除操作后仍可以访问释放的动态分配的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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