为什么我需要删除[]? [英] Why do I need to delete[]?

查看:121
本文介绍了为什么我需要删除[]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的函数:

int main()
{
    char* str = new char[10];

    for(int i=0;i<5;i++)
    {
        //Do stuff with str
    }

    delete[] str;
    return 0;
}




  1. 为什么需要删除 str 如果我要结束程序吗?
    我不在乎,如果我只是要退出,那记忆会去一个充满独角兽的土地,对不对?

  1. Why would I need to delete str if I am going to end the program anyways? I wouldn't care if that memory goes to a land full of unicorns if I am just going to exit, right?

这只是好习惯吗?

它有更深的后果吗?


推荐答案

如果其实你的问题真的是我有这个琐碎的程序,是不是我不释放几个字节之前它退出?答案是肯定的,那很好。在任何现代的操作系统,这将是很好。程序是微不足道的;它不像你要把它放入起搏器或运行与这个东西丰田凯美瑞的制动系统。如果唯一的客户是你,那么你唯一可能受到影响的人是你。

If in fact your question really is "I have this trivial program, is it OK that I don't free a few bytes before it exits?" the answer is yes, that's fine. On any modern operating system that's going to be just fine. And the program is trivial; it's not like you're going to be putting it into a pacemaker or running the braking systems of a Toyota Camry with this thing. If the only customer is you then the only person you can possibly impact by being sloppy is you.

然后,当你开始推广到不平凡的案例时,问题出现从这个问题的答案询问一个小事情。

The problem then comes in when you start to generalize to non-trivial cases from the answer to this question asked about a trivial case.

因此,让我们提出两个关于一些不平凡案件的问题。

So let's instead ask two questions about some non-trivial cases.


我有一个长期运行的服务,以复杂的方式分配和释放内存,也许涉及多个分配器击中多个堆。在正常模式下关闭我的服务是一个复杂和耗时的过程,涉及确保外部状态 - 文件,数据库等 - 始终关闭。我应该确保我分配的内存的每个字节在我关闭之前被释放了吗?

I have a long-running service that allocates and deallocates memory in complex ways, perhaps involving multiple allocators hitting multiple heaps. Shutting down my service in the normal mode is a complicated and time-consuming process that involves ensuring that external state -- files, databases, etc -- are consistently shut down. Should I ensure that every byte of memory that I allocated is deallocated before I shut down?

是的,我会告诉你为什么。一个长期运行的服务可能发生的最糟糕的事情之一是如果它意外泄漏内存。即使是微小的泄漏也可能导致巨大的泄漏。用于查找和修复内存泄漏的标准技术是调整分配堆,以便在关闭时,它们记录所有未分配的资源。除非你喜欢追逐很多误报,并且在调试器中花费大量时间,否则总是释放你的内存,即使这样做绝对不是必要的。

Yes, and I'll tell you why. One of the worst things that can happen to a long-running service is if it accidentally leaks memory. Even tiny leaks can add up to huge leaks over time. A standard technique for finding and fixing memory leaks is to instrument the allocation heaps so that at shutdown time they log all the resources that were ever allocated without being freed. Unless you like chasing down a lot of false positives and spending a lot of time in the debugger, always free your memory even if doing so is not strictly speaking necessary.

用户已经期望关闭服务可能需要几十亿纳秒的时间,所以如果你对虚拟分配器造成一点额外的压力,确保一切都被清除,那么他们会怎么样?这只是你为大型复杂软件支付的价格。而不是你一直关闭服务,所以再次,谁在乎它的几毫秒是否会比它慢?

The user is already expecting that shutting the service down might take billions of nanoseconds so who cares if you cause a little extra pressure on the virtual allocator making sure that everything is cleaned up? This is just the price you pay for big complicated software. And it's not like you're shutting down the service all the time, so again, who cares if its a few milliseconds slower than it could be?


我有同样长期服务。如果我检测到我的内部数据结构之一已损坏,我希望快速失败。程序处于未定义的状态,它可能以提升的权限运行,我将假设如果我检测到损坏的状态,这是因为我的服务正在积极地被敌对方攻击。最安全的事情是立即关闭服务。我宁愿允许攻击者拒绝为客户提供服务,而不是让服务继续存在并进一步损害我的用户数据的风险。在这种紧急关机情况下,我应该确保我分配的内存的每个字节被释放?

I have that same long-running service. If I detect that one of my internal data structures is corrupt I wish to "fail fast". The program is in an undefined state, it is likely running with elevated privileges, and I am going to assume that if I detect corrupted state, it is because my service is actively being attacked by hostile parties. The safest thing to do is to shut down the service immediately. I would rather allow the attackers to deny service to the clients than to risk the service staying up and compromising my users' data further. In this emergency shutdown scenario should I make sure that every byte of memory I allocated is freed?

当然不是。操作系统将为你照顾。如果您的堆已损坏,攻击者可能希望您释放内存作为其漏洞的一部分。每毫秒计数。为什么你会打扫抛光门把手和擦厨房,然后你把一个战术nuke在建设?

Of course not. The operating system is going to take care of that for you. If your heap is corrupt, the attackers may be hoping that you free memory as part of their exploit. Every millisecond counts. And why would you bother polishing the doorknobs and mopping the kitchen before you drop a tactical nuke on the building?

因此,问题的答案在我的程序退出之前我应该​​释放内存吗?是它取决于你的程序做什么。

So the answer to the question "should I free memory before my program exits?" is "it depends on what your program does".

这篇关于为什么我需要删除[]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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