Visual C ++本机内存管理最佳实践 [英] Visual c++ native memory management best practices

查看:128
本文介绍了Visual C ++本机内存管理最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是老C#程序员和C程序员(没有动态内存分配),但是想学习一些有关Visual C ++编程的知识.困扰我的问题与C ++中的内存管理有关.在C#中,垃圾收集器负责内存管理,但是在C ++中,必须建立一些有关谁负责释放分配的内存的规则.我有一些来自C#的典型场景:

I am an old C# programmer and a C programmer (without dynamic memory allocation), but would like to learn a bit about Visual C++ programming. The question that bothers me is related to memory management in C++. In C#, garbage collector takes care of the memory management, but in C++, it is necessary that some rules are established regarding who is responsible for freeing the allocated memory. I have some typical scenarios from C#:

  1. 对象放入某种容器中.谁负责释放内存.如果几个类共享同一个对象怎么办?

  1. Object is put in a container of some kind. Who is responsible for freeing the memory. What if several classes share the same object?

工厂模式.我喜欢使用类的层次结构,其中父类具有创建子对象的方法?

Factory pattern. I like using a hierarchy of classes, where the parent class has a method for creating child objects?

是否可以向调用方法建议返回的对象归被调用方/调用方所有.

Is there a way to suggest to the calling method that the returned object is in ownership of the callee/caller.

我想听听一些有关此的好提示.

I would like to hear some good tips about this.

推荐答案

如果正确编写代码,则不必担心,至少不必直接担心.有一些可用的库功能可以完全自动为您处理内存管理和其他资源管理,因此您不必这样做.

If you write your code correctly, you won't have to worry about this, at least not directly. There are library facilities available to you that handle memory management and other resource management for you, completely automatically, so that you don't have to.

C ++提供的功能远胜于垃圾收集器:它提供了对所有对象的确定性销毁,并且确定性销毁可用于自动管理每个资源的生命周期,这与垃圾收集(在许多常见实现中)不同,后者只能使您自动管理内存,并让您手动管理需要确定性清理的所有其他资源.

C++ provides something far better than a garbage collector: it provides deterministic destruction of all objects, and that deterministic destruction can be used to automatically manage the lifetime of every resource, unlike garbage collection which (in many common implementations) only allows you to manage memory automatically and makes you manually manage all other resources that require deterministic cleanup.

如果动态分配对象,请使用智能指针来管理其寿命.如果不需要共享所有权,则可以使用std::unique_ptr,它允许您将所有权从一个所有者转移到另一个所有者.如果确实需要共享所有权,则可以使用std::shared_ptr,它使用引用计数技术来维护共享所有权.

If you dynamically allocate an object, use a smart pointer to manage its lifetime. If you don't need to share ownership, then you can use a std::unique_ptr, which allows you to transfer ownership from one owener to another. If you do need to share ownership, you can use a std::shared_ptr, which uses a reference counting technique to maintain shared ownership.

要记住两个重要规则:

  • 如果必须在C ++程序中编写delete,则代码肯定是错误的. C ++为包括内存在内的所有资源提供了自动生命周期管理,您应该利用它的. delete应该出现的唯一位置是在实现资源拥有容器的库代码中,并且可能在稀有的低级代码中出现.

  • If you have to write delete in your C++ program, the code is almost certainly wrong. C++ provides automatic lifetime management for all resources, including memory, and you should take advantage of it. The only place delete should appear is in library code where resource-owning containers are implemented and potentially in rarer, low-level code.

在可能的情况下首选处理对象,而不是指向对象的指针.在可能的情况下,应避免显式动态分配. C ++与C#和Java这样的语言不同,大多数对象都是在堆上创建的.在C ++中,通常最好在堆栈上创建对象(使用自动变量),然后按值返回它们.

Prefer to deal with objects, rather than pointers to objects, wherever possible. Wherever possible, you should avoid explicit dynamic allocation. C++ isn't like languages like C# and Java where most objects get created on the heap. It is often better in C++ to create objects on the stack (using automatic variables) and return them by value.

要回答您的特定情况:

对象被放入某种容器中.谁负责释放内存.如果几个类共享同一个对象怎么办?

Object is put in a container of some kind. Who is responsible for freeing the memory. What if several classes share the same object?

您应该尽可能将对象本身存储在容器中,而不是指向对象的指针.如果出于某种原因需要存储指向对象的指针,则应使用智能指针的容器(例如std::vector).如果容器对动态分配的对象拥有唯一的所有权,则可以使用std::vector<std::unique_ptr<T>>;否则,可以使用std::vector<std::unique_ptr<T>>.如果容器要共享对象的所有权,则可以使用std::vector<std::shared_ptr<T>>.

You should prefer, wherever possible, to store the objects themselves in the container, not pointers to the objects. If you do for some reason need to store pointers to objects, you should use a container (e.g. a std::vector) of smart pointers. If the container has sole ownership of the dynamically allocated objects, you can use a std::vector<std::unique_ptr<T>>; if the container is going to share ownership of the objects, you can use a std::vector<std::shared_ptr<T>>.

工厂模式.我喜欢使用类的层次结构,其中父类具有创建子对象的方法?

Factory pattern. I like using a hierarchy of classes, where the parent class has a method for creating child objects?

这与以前的情况没有什么不同:如果您有一棵树,那么使用std::vector<T>(如果需要动态分配的子代,则使用std::vector<std::unique_ptr<T>>)来拥有这些子代非常简单.

This is no different from the previous scenario: if you have a tree, it's quite simple to use a std::vector<T> (or a std::vector<std::unique_ptr<T>> if you need dynamically allocated children) to own the children.

是否可以向调用方法建议返回的对象归被调用方/调用方所有.

Is there a way to suggest to the calling method that the returned object is in ownership of the callee/caller.

如果对象仅由被调用方所有(例如,在std::unique_ptr中),则可以返回该智能指针;否则,可以返回该智能指针.这样做会将所有权转移给呼叫者.

If the object is solely owned by the callee (e.g. in a std::unique_ptr), then you can return that smart pointer; doing so will transfer ownership to the caller.

如果有对象的共享所有权(例如,在std::shared_ptr中),则返回智能指针将使调用者成为所有者之一;最后一个放弃其所有权的所有者(通过销毁或重置拥有该对象的std::shared_ptr)会自动销毁该对象.

If there is shared ownership of the object (e.g. in a std::shared_ptr), then returning the smart pointer will make the caller one of the owners; the last owner to relinquish its ownership (by destroying or otherwise resetting its std::shared_ptr that owns the object) automatically destroys the object.

这篇关于Visual C ++本机内存管理最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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