c ++ 11功能的新功能,是否正确使用了shared_ptr? [英] New to c++11 features, proper use of shared_ptr?

查看:94
本文介绍了c ++ 11功能的新功能,是否正确使用了shared_ptr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我的理解是,当对象的最后一个剩余所有者被销毁或重新分配时,shared_ptr会自动从内存中取消分配(似乎太好了,难以置信吗?),当许多实例共享同一对象时,它很有用。是吗?

So my understanding is that a shared_ptr automatically deallocates from memory when the last remaining owner of the object is destroyed or reassigned, (Seems too good to be true?) and it's useful when many instances may be sharing the same object. Correct?

因此,在我的情况下,我正在制作2D平铺的世界,所以我要在屏幕上绘制许多相同的纹理。

So in my case, I'm making a 2d tiled world, so I'm drawing many of the same texture to the screen.

我有

std::map<int, shared_ptr<Tile>> Tiledb;

存储所有图块。这个想法是只加载一次纹理,然后我可以根据需要多次渲染它。然后,一旦游戏结束,我就叫

to store all the tiles. The idea is to only load the texture in once and then i can render it as many times as I want. Then once the game ends I call

Tiledb.clear();

这会自动释放所有内存吗?我非常习惯常规指针,以至于这似乎很神奇,而且坦率地说,太容易了。我以为这是怎么回事是错误的吗?使用shared_ptrs有什么缺点吗?我只是惊讶这存在哈哈。

And this frees all the memory automatically? I'm so conditioned to regular pointers that this just seems magical, and quite frankly, too easy. Am I wrong in thinking this is how it works? Is there any downside to using shared_ptrs? I'm just amazed that this exists haha.

感谢您提供任何信息。

Thanks for any information.

推荐答案


...当许多实例共享同一对象时,它很有用。是吗?

...it's useful when many instances may be sharing the same object. Correct?

不完全是。当许多实例可能拥有同一对象时,这很有用。共享不足以使用 std :: shared_ptr 来证明其合理性,因为使用共享确实存在一些开销。

Not exactly. It is useful when many instances may be owning the same object. Sharing is not enough to justify using a std::shared_ptr as there is certainly some overhead to using it.

创建动态资源时,您需要考虑所有权,即。 谁负责删除它?应该负责删除资源的对象应该使用某种智能指针(或容器)来管理资源。

When you create dynamic resources you need to think about ownership ie. who is responsible for deleting it? The object that should be responsible for deleting the resource should manage the resource using some kind of smart pointer (or a container).

如果只有一个对象负责确定何时必须删除资源,则使用 std :: unique_ptr 。如果其他对象/功能需要共享对资源的访问权限,但永远不会负责删除资源,则向它们传递引用原始指针

If only one object is responsible for deciding when the resource must be deleted then use a std::unique_ptr. If other objects/functions need to share access to the resource but will never be responsible for deleting it, then pass them a reference or a raw pointer to the resource.

使用 std :: shared_ptr 的时间是您不知道哪个共享资源的对象将是需要删除它的对象。在这种情况下,每个对象都应通过持有 std :: shared_ptr 来拥有资源的所有权

The time to use a std::shared_ptr is when you can not know which of the objects that are sharing the resource will be the one that needs to delete it. In that case each object should hold ownership of the resource by holding a std::shared_ptr.

即使几个对象通过 std :: shared_ptr 共享所有权,它们仍应仅传递引用或指向不需要所有权权限的对象/函数的原始指针

And even when several objects share ownership through a std::shared_ptr they should still only pass a reference or a raw pointer to objects/functions that do not need ownership rights.

传递 std :: stared_ptr 会在Willy-nilly(不需要它们的地方)进行操作,因为它们可能会遇到 Java内存泄漏问题。那是因为对象的某些引用保留在软件的被遗忘的部分中,因此对象永不死亡。他们会逐渐积累并吞噬您的记忆。

Another problem with passing std::stared_ptr round willy-nilly (where they are not needed) is that they can suffer from the Java memory leak problem. That is when objects never die because some reference to them remains in a forgotten part of the software. They can gradually accumulate and eat away your memory.

通常,您应该喜欢将资源保存在容器中,例如 std :: vector std :: map

Typically you should prefer keeping your resources in a container like a std::vector or a std::map:

std::map<int, Tile> Tiledb;

容器管理 Tile的销毁,因此不需要智能指针

但是,如果您使用的是多态的 平铺对象,则需要使用 pointer 来存储它们。为此,更喜欢使用 std :: unique_ptr

If, however, you were using polymorphic Tile objects then you would need to store them using a pointer. For this prefer to use std::unique_ptr:

// When the Tiles are no longer needed after the map is destroyed
std::map<int, std::unique_ptr<Tile>> Tiledb;

如果其他对象需要继续访问 Tile 地图之后的对象被销毁,然后可能合适的是 std :: shared_ptr

If other objects need to keep accessing the Tile objects after the map is destroyed then a std::shared_ptr may be appropriate:

// only when Tiles need to keep living after the map is destroyed.
std::map<int, std::shared_ptr<Tile>> Tiledb; 

这篇关于c ++ 11功能的新功能,是否正确使用了shared_ptr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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