我知道 C 语言中有 malloc/free 之类的东西,以及 C++ 语言中用于内存管理的 new/using-a-destructor 之类的东西,但我想知道为什么这些语言没有自动内存的“新更新”管理(垃圾收集)? java有垃圾收集功能,为什么没有C和C++
有人能简单解释一下吗?
最佳答案
C 和 C++ 被设计为接近金属,并且在设计它们时,垃圾收集几乎不是一种选择。当您想到垃圾收集时,必须有一个用于引用计数并跟踪分配的数据结构,这反过来又会增加内存和性能的开销。当设计 C 和 C++ 时,这些并不是目标,因为它更接近硬件。
比亚尼·斯特鲁斯特鲁普 said :
I had hoped that a garbage collector which could be optionally enabled would be part of C++0x, but there were enough technical problems that I have to make do with just a detailed specification of how such a collector integrates with the rest of the language, if provided. As is the case with essentially all C++0x features, an experimental implementation exists.
关于Java 有垃圾收集,为什么 C 和 C++ 语言没有垃圾收集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29789669/