动态内存分配“删除”

发布于 2024-10-15 01:15:16 字数 433 浏览 6 评论 0原文

如果我为 int 对象动态分配内存位置,如下所示:

int *x = new int;

完成后,想要释放堆上的内存,我将执行以下操作:

delete x;

现在,如果我执行以下操作:

x = NULL;

将会 x< /code> 指向另一个地址? 更新:另一个而不是许多

说我没有做x = NULL并做了另一个< /strong> 删除x;,会发生什么?

If I allocated a memory location for an int object dynamically as follows:

int *x = new int;

After done with it, and want to free the memory on the heap, I will do the following:

delete x;

Now, if I did not do the following:

x = NULL;

Will x be pointing to another address? UPDATE: another instead of many

Say I didn't do x = NULL and made another delete x;, what will happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

揽清风入怀 2024-10-22 01:15:16
  1. 删除 x 后,您对 x 所做的任何操作(除了 = NULL - 在我看来,这是不好的做法)都是未定义的。
  2. 双重删除=灾难。

注意:某些运行时系统会
保护您免受某些非常简单的影响
双重删除的情况。根据
细节,如果你
碰巧在其中之一上运行
系统,如果没有人部署
您在另一个系统上的代码
以不同的方式处理事情,如果你
正在删除一些不存在的内容
有一个析构函数,如果你不这样做
两者之间有什么重要的事情
删除,如果没有人改变
你的代码来做一些重要的事情
在两次删除之间,如果您的
线程调度程序(您可以通过它
可能无法控制!)不
碰巧在两者之间交换线程
删除 and if、and if、and if。所以
回到墨菲:因为它可能会出错,
会的,而且会出错
最糟糕的时刻。

https://isocpp.org/wiki/faq/freestore-mgmt

  1. Anything you do with x (apart from = NULL - which is, imo, bad practice) after you delete it is undefined.
  2. double-delete = disaster.

Note: some runtime systems will
protect you from certain very simple
cases of double delete. Depending on
the details, you might be okay if you
happen to be running on one of those
systems and if no one ever deploys
your code on another system that
handles things differently and if you
are deleting something that doesn't
have a destructor and if you don't do
anything significant between the two
deletes and if no one ever changes
your code to do something significant
between the two deletes and if your
thread scheduler (over which you
likely have no control!) doesn't
happen to swap threads between the two
deletes and if, and if, and if. So
back to Murphy: since it can go wrong,
it will, and it will go wrong at the
worst possible moment.

https://isocpp.org/wiki/faq/freestore-mgmt

_失温 2024-10-22 01:15:16

删除之后,指针通常仍包含(现在空闲)内存的地址。第二个 delete 给出了未定义的行为,所以不要这样做。

After the delete, the pointer will typically still contain the address of the (now free) memory. The second delete gives undefined behavior, so don't do that.

瀟灑尐姊 2024-10-22 01:15:16

它将调用未定义的行为。如果您不执行 x=NULL ,则 x 将指向无效的内存位置,如果您尝试使用该位置,将导致未定义的行为。

It will invoke undefined behavior. If you don't do x=NULL then x will be pointing to an invalid memory location which if you try to use will cause undefined behavior.

司马昭之心 2024-10-22 01:15:16

类型:

int main(){
  int* i = new int;
  std::cout << i << std::endl;
  delete i;
  std::cout << i << std::endl;
  delete i;
}

结果:

0x8e19008

0x8e19008

** glibc 检测到 ** ./a.out:双重释放或损坏(fasttop):0x08e19008 *

如您所见,地址保持不变,但第二次删除结束在运行时错误中。行为在细节上可能取决于环境,但作为一般规则,它是行不通的。

type:

int main(){
  int* i = new int;
  std::cout << i << std::endl;
  delete i;
  std::cout << i << std::endl;
  delete i;
}

result:

0x8e19008

0x8e19008

** glibc detected ** ./a.out: double free or corruption (fasttop): 0x08e19008 *

as you see, the address stays the same, but the second delete ends in a runtime error. behaviour may in detail depend on environment, but as a general rule it will not work.

江城子 2024-10-22 01:15:16

对已删除的内存调用删除将导致未定义的行为。一般来说,你的程序会崩溃。

删除 x 后,它将指向“编译器相关”。大多数编译器只会让它指向调用删除之前的位置。但该内存地址不再有效,因此不应被调用。

出于同样的原因,如果需要的话,必须非常明智和谨慎地使用“删除此”。 :-)

calling delete on already deleted memory will cause undefined behaviour. Generally, your program will crash.

After deleting x, where it will be pointing is "compiler dependent". Most of the compilers will just let it point to where it was, before delete was called. But that memory address is no more valid, and hence should not be called.

For this same reason, "delete this" must be used very judiciously and carefully, if at all needed. :-)

A君 2024-10-22 01:15:16

第二次删除未定义。

附带说明一下,有一些工具可以检测双重删除。最好的之一是 Valgrind

The secondth delete is undefined.

As a side note, there are tools to detect a double deletion. One of the best one around is Valgrind.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文