不释放内存是否可以接受

2021-12-31 00:00:00 oop c memory-management memory-leaks c++

我正在处理一个应该从命令行使用以下语法的项目:

I'm working on a project that is supposed to be used from the command line with the following syntax:

program-name input-file

程序应该处理输入,计算一些东西并在标准输出上输出结果.

The program is supposed to process the input, compute some stuff and spit out results on stdout.

我选择的语言是 C++,有几个原因我不愿意争论.计算阶段将是高度象征性的(想想编译器),并将使用非常复杂的动态分配的数据结构.特别是,它不适合 RAII 风格的编程.

My language of choice is C++ for several reasons I'm not willing to debate. The computation phase will be highly symbolic (think compiler) and will use pretty complex dynamically allocated data structures. In particular, it's not amenable to RAII style programming.

我想知道忘记释放内存是否可以接受,因为我希望整个计算消耗的内存少于可用内存,并且操作系统可以在程序完成后一步自由地回收所有内存(假设程序在几秒钟内终止).您对此有何看法?

I'm wondering if it is acceptable to forget about freeing memory, given that I expect the entire computation to consume less than the available memory and that the OS is free to reclaim all the memory in one step after the program finishes (assume program terminates in seconds). What are your feeling about this?

作为备用计划,如果我的项目需要作为服务器或交互运行,我想我总是可以将垃圾收集器重新安装到源代码中.有没有人有使用 C++ 垃圾收集器的经验?他们工作得好吗?

As a backup plan, if ever my project will require to run as a server or interactively, I figured that I can always refit a garbage collector into the source code. Does anyone have experience using garbage collectors for C++? Do they work well?

推荐答案

在问题描述的具体情况下应该不会造成任何问题.

It shouldn't cause any problems in the specific situation described the question.

然而,这并不完全正常.静态分析工具会抱怨它.最重要的是,它会养成坏习惯.

However, it's not exactly normal. Static analysis tools will complain about it. Most importantly, it builds bad habits.

相关文章