标准 C++ 中的垃圾收集是自动的吗?

Is garbage collection automatic in standard C++?(标准 C++ 中的垃圾收集是自动的吗?)
本文介绍了标准 C++ 中的垃圾收集是自动的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,在标准 C++ 中,每当您使用 new 运算符时,您还必须在某些时候使用 delete 运算符以防止内存泄漏.这是因为 C++ 中没有垃圾收集.在 .NET 中,垃圾收集是自动的,因此无需担心内存管理.我的理解正确吗?谢谢.

From what I understand, in standard C++ whenever you use the new operator you must also use the delete operator at some point to prevent memory leaks. This is because there is no garbage collection in C++. In .NET garbage collection is automatic so there is no need to worry about memory management. Is my understanding correct? Thanks.

推荐答案

对它的长答案是,每次调用 new 时,在某个地方,不知何故,delete必须调用,或者其他一些释放函数(取决于内存分配器等)

The long answer to it is that for every time new is called, somewhere, somehow, delete must be called, or some other deallocation function (depends on the memory allocator etc.)

但您不必是提供 delete 调用的人:

But you don't need to be the one supplying the delete call:

  1. 有 C++ 垃圾收集,形式为 Hans-Boehm Garbage Collector.可能还有其他垃圾收集库.
  2. 您可以使用智能指针,它使用 RAII(如果指针允许共享访问,则使用引用计数)来确定何时删除对象.一个好的智能指针库是 Boost 的 智能指针.在绝大多数情况下,智能指针可以替代原始指针.
  3. 一些应用程序框架,例如 Qt,构建对象树,这样框架的对象就存在父子关系堆分配的对象.因此,只需在对象上调用 delete,其所有子对象也将自动被 deleted.
  1. There is garbage collection for C++, in the form of the Hans-Boehm Garbage Collector. There is also probably other garbage collection libraries.
  2. You can use smart pointers, which use RAII (and reference counting if the pointer allows shared access) to determine when to delete the object. A good smart pointer library is Boost's smart pointer. Smart pointers in the vast majority of cases can replace raw pointers.
  3. Some application frameworks, like Qt, build object trees, such that there is a parent child relationship for the framework's heap allocated objects. As a result, all is needed is for a delete to be called on an object, and all its children will automatically be deleted as well.

如果您不想使用任何这些技术来防止内存泄漏,您可以尝试使用内存检查工具.Valgrind 特别好,虽然它只适用于 Linux

If you don't want to use any of these techniques, to safeguard against memory leaks, you can try using a memory checking tool. Valgrind is particularly good, although it only works on Linux

对于 .NET,是的,使用 gcnew 分配意味着内存由 .NET 跟踪,因此不会泄漏.然而,其他资源,如文件句柄等,不由 GC 管理.

As for .NET, yes, allocating using gcnew means that the memory is tracked by .NET, so no leaks. Other resources however, like file handles etc. are not managed by the GC.

这篇关于标准 C++ 中的垃圾收集是自动的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)