我应该在程序结束时删除 C++ 中的大树集合还是将其留给操作系统? [英] Should I delete big tree collections in C++ at the end of program or leave that to OS?

查看:44
本文介绍了我应该在程序结束时删除 C++ 中的大树集合还是将其留给操作系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个有点愚蠢的问题 - 我应该在程序结束时在巨大的地图/集合上调用 delete 吗?
假设整个程序都需要地图/集(delete 是返回前的最后一行)并且它的大小非常大(> 4GB).delete 调用需要很长时间,从我的角度来看没有任何价值(内存不能很快释放),我错了吗?如果是,为什么?

This might be a bit stupid question - should I call delete on huge map/set at the end of the program?
Assuming the map/set is needed throughout all program (delete is last line before return) and it's size is really huge (> 4GB). The delete call take a long time and from my perspective does not have any value(memory cannot be released any sooner), am I wrong? If so, why?

推荐答案

C 和 C++ 标准不保证程序退出后会发生什么.包括不保证任何东西都被清理干净.例如,一些较小的实时操作系统不会执行自动清理.所以至少在理论上,你的程序绝对应该deletenew的所有内容,以履行它作为一个可以永远运行的完整和可移植程序的义务.

There is no guarantee in the C and C++ standards of what happens after your program exits. Including no guarantee that anything is cleaned up. Some smaller real-time OS' for example, will not perform automatic cleanup. So at least in theory, your program should definitely do delete everything you new to fulfil it's obligation as a complete and portable program that can run forever.

也有可能有人拿走了你的代码并在它周围放置了一个循环,这样你的树现在被创建了一百万次,然后去找你,带来了值得信赖的说服者",又名棒球棒,当他们发现为什么它现在在 500 次迭代后内存不足.

It is also possible that someone takes your code and puts a loop around it, so that your tree is now created a million times, and then goes to find you, bringing along the "trusty convincer", aka base-ball bat, when they find out WHY it's now running out of memory after 500 iterations.

当然,就像所有事情一样,这可以通过许多不同的方式进行争论,这实际上取决于您要实现的目标,为什么要编写程序等.我的编译器项目像筛子一样泄漏内存,因为我使用正是您描述的内存管理方法(部分原因是跟踪每个动态分配对象的生命周期非常困难,部分原因是我不会被打扰".我敢肯定如果有人真的想要一个好的 pascal 编译器,他们不会去我的代码anwyay).

Of course, like all things, this can be argued many different ways, and it really depends on what you are trying to achieve, why you are writing the program etc. My compiler project leaks memory like a sieve, because I use exactly the method of memory management that you describe (partly because tracking the lifetime of each dynamically allocated object is quite difficult, and partly because "I can't be bothered". I'm sure if someone actually wants a good pascal compiler, they won't go for my code anwyay).

实际上,我的编译器项目构建了许多不同的数据结构,其中一些是树、数组等,但基本上它们都没有执行任何后记清理.修复起来并不像构建一个需要删除每个节点的大树那样简单.然而,从概念上讲,这一切都归结为做清理"或不做清理",而这又归结为好吧,谁将使用/修改代码,你对它会使用的环境了解多少?跑进".

Actually, my compiler project builds many different data structures, some of which are trees, arrays, etc, but basically none of them perform any cleanup afterwords. It's not as simple to fix as the case of building a large tree that need each node deleting. However, conceptually, it all boils down to "do cleanup" or "not do cleanup", and that in turn comes down to "well, who is going to use/modify the code, and what do you know about the environment it will run in".

这篇关于我应该在程序结束时删除 C++ 中的大树集合还是将其留给操作系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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