<small id='v20zr'></small><noframes id='v20zr'>

    <bdo id='v20zr'></bdo><ul id='v20zr'></ul>
<legend id='v20zr'><style id='v20zr'><dir id='v20zr'><q id='v20zr'></q></dir></style></legend>
<tfoot id='v20zr'></tfoot>
<i id='v20zr'><tr id='v20zr'><dt id='v20zr'><q id='v20zr'><span id='v20zr'><b id='v20zr'><form id='v20zr'><ins id='v20zr'></ins><ul id='v20zr'></ul><sub id='v20zr'></sub></form><legend id='v20zr'></legend><bdo id='v20zr'><pre id='v20zr'><center id='v20zr'></center></pre></bdo></b><th id='v20zr'></th></span></q></dt></tr></i><div id='v20zr'><tfoot id='v20zr'></tfoot><dl id='v20zr'><fieldset id='v20zr'></fieldset></dl></div>
    1. 标准 C++ 中的垃圾收集是自动的吗?

      时间:2023-09-13

      <small id='kwGUu'></small><noframes id='kwGUu'>

          <tbody id='kwGUu'></tbody>

            <bdo id='kwGUu'></bdo><ul id='kwGUu'></ul>
            • <tfoot id='kwGUu'></tfoot>

              1. <i id='kwGUu'><tr id='kwGUu'><dt id='kwGUu'><q id='kwGUu'><span id='kwGUu'><b id='kwGUu'><form id='kwGUu'><ins id='kwGUu'></ins><ul id='kwGUu'></ul><sub id='kwGUu'></sub></form><legend id='kwGUu'></legend><bdo id='kwGUu'><pre id='kwGUu'><center id='kwGUu'></center></pre></bdo></b><th id='kwGUu'></th></span></q></dt></tr></i><div id='kwGUu'><tfoot id='kwGUu'></tfoot><dl id='kwGUu'><fieldset id='kwGUu'></fieldset></dl></div>
                <legend id='kwGUu'><style id='kwGUu'><dir id='kwGUu'><q id='kwGUu'></q></dir></style></legend>
              2. 本文介绍了标准 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# 中创建一个真正的弱键字典?

                相关文章

              3. <i id='oiagL'><tr id='oiagL'><dt id='oiagL'><q id='oiagL'><span id='oiagL'><b id='oiagL'><form id='oiagL'><ins id='oiagL'></ins><ul id='oiagL'></ul><sub id='oiagL'></sub></form><legend id='oiagL'></legend><bdo id='oiagL'><pre id='oiagL'><center id='oiagL'></center></pre></bdo></b><th id='oiagL'></th></span></q></dt></tr></i><div id='oiagL'><tfoot id='oiagL'></tfoot><dl id='oiagL'><fieldset id='oiagL'></fieldset></dl></div>

                <small id='oiagL'></small><noframes id='oiagL'>

                1. <legend id='oiagL'><style id='oiagL'><dir id='oiagL'><q id='oiagL'></q></dir></style></legend>
                2. <tfoot id='oiagL'></tfoot>
                    <bdo id='oiagL'></bdo><ul id='oiagL'></ul>