十大愚蠢的错误,以避免与C ++ 11智能指针

21 篇文章 0 订阅

Top 10 dumb mistakes to avoid with C++ 11 smart pointers

I love the new C++ 11 smart pointers. In many ways, they were a godsent for many folks who hate managing their own memory. In my opinion, it made teaching C++ to newcomers much easier.

我喜欢新的C ++ 11智能指针。在很多方面,他们是为godsent恨谁管理自己的记忆很多人。在我看来,它使教学C ++新人容易得多。

However, in the two plus years that I've been using them extensively, I've come across multiple cases where improper use of the C++ 11 smart pointers made the program inefficient or simply crash and burn. I've catalogued them below for easy reference. 

然而,在我使用它们广泛地被两个多年,我已经遇到许多情况下,不当使用C ++ 11智能指针所生成的程序效率低下或者干脆和好如初。我下面编目他们,以供参考。

Before we begin, let's take a look at a simple Aircraft class we'll use to illustrate the mistakes.

在我们开始之前,让我们来看看,我们将用它来说明错误,一个简单的飞机类。

class Aircraft
{
private:
	string m_model;
 
public:
 
	int m_flyCount;
 
	weak_ptr<aircraft> myWingMan;
 
	void Fly()
	{
		cout << "Aircraft type" << m_model << "is flying !" << endl;
	}
 
	Aircraft(string model)
	{
		m_model = model;
		cout << "Aircraft type " << model << " is created" << endl;
	}
 
	Aircraft()
	{
		m_model = "Generic Model";
		cout << "Generic Model Aircraft created." << endl;
	}
 
	~Aircraft()
	{
		cout << "Aircraft type  " << m_model << " is destroyed" << endl;
	}
 
};
</aircraft>
http://blog.csdn.net/sergeycao/article/details/52538123

Mistake # 1 : Using a shared pointer where an unique pointer suffices !!!

错误#1:使用共享指针,其中一个唯一的指针就足够了!

I’ve recently been working in an inherited codebase which uses a shared_ptr for creating and managing every object. When I analyzed the code, I found that in 90% of the cases, the resource wrapped by the shared_ptr is not shared.

我最近一直在使用,用于创建和管理每个对象一个shared_ptr继承的代码库工作。当我分析的代码,我发现,在90%的情况,由shared_ptr的包装资源不共享。

This is problematic because of two reasons:

1. If you have a resource that’s really meant to be owned exclusively, using a shared_ptr  instead of a unique_ptr makes the code susceptible to unwanted resource leaks and bugs.

  • Subtle Bugs: Just imagine if you never imagined a scenario where the resource is shared out by some other programmer by  assigning it to another shared pointer which inadvertently modifies the resource !
  • Unnecessary Resource Utilization: Even if the other pointer does not modify the shared resource, it might hang on to it far longer than necessary thereby hogging your RAM unnecessarily even after the original shared_ptr goes out of scope.

2. Creating a shared_ptr is more resource intensive than creating a unique_ptr.

  • A shared_ptr needs to maintain the threadsafe refcount of objects it points to and a control block under the covers which makes it more heavyweight than an unique_ptr.
这是由于两方面的原因存在问题:
1.如果您有这确实意味着使用一个shared_ptr代替的unique_ptr使得代码容易受到不必要的资源泄漏和错误应该只拥有一个资源。
微妙的错误:试想一下,如果你从来没有想过当资源被其他程序员共享出来的情景将其分配到另一个共享指针,无意中修改资源!
不必要的资源利用率​​:即使其他指针不修改共享资源,它可能更长的时间挂在比必要占用,从而你的内存不必要原来的shared_ptr超出范围后还是一样。
2.创建一个shared_ptr更多的资源比创建的unique_ptr密集。
一个shared_ptr需要维持,这使得它比的unique_ptr更重量级的幕后对象的引用计数线程它指向和控制模块。
建议 - 默认情况下,你应该使用的unique_ptr。如果要求上来以后共享资源所​​有权,可以随时将其更改为一个shared_ptr。

Recommendation – By default, you should use a unique_ptr. If a requirement comes up later to share the resource ownership, you can always change it to a shared_ptr.


  • http://blog.csdn.net/sergeycao/article/details/52538123
    

    Mistake # 2 : Not making resources/objects shared by shared_ptr threadsafe !

    错误#2:不使由shared_ptr的线程共享资源/对象!

    Shared_ptr allows you to share the resource thorough multiple pointers which can essentially be used from multiple threads. It’s a common mistake to assume that wrapping an object up in a shared_ptr makes it inherently thread safe. It’s still your responsibility to put synchronization primitives around the shared resource managed by a shared_ptr.

    shared_ptr的可以让你分享这基本上可以从多个线程使用的资源彻底多个指针。这是一个常见的​​错误假设,在一个shared_ptr的包装对象多达使得它固有线程安全的。它仍然是你的责任,把周围由一个shared_ptr管理的共享资源同步原语。
    建议 - 如果你不共享多个线程之间的资源规划,使用的unique_ptr。

    Recommendation – If you do not plan on sharing the resource between multiple threads, use a unique_ptr.

    http://blog.csdn.net/sergeycao/article/details/52538123
    

    Mistake # 3 : Using auto_ptr !

    错误#3:使用auto_ptr的!

    The auto_ptr feature was outright dangerous and has now been deprecated. The transfer of ownership executed by the copy constructor when the pointer is passed by value can cause fatal crashes in the system when the original auto pointer gets dereferenced again. Consider an example:

    auto_ptr的特点是彻头彻尾的危险,现在已经被废弃了。由当指针是按值传递的拷贝构造函数执行所有权的转移,可以在原有的自动指针被解除引用再次在系统造成致命的崩溃。考虑一个例子:

    int main()
    {
    	auto_ptr<aircraft> myAutoPtr(new Aircraft("F-15"));
    	SetFlightCountWithAutoPtr(myAutoPtr); // Invokes the copy constructor for the auto_ptr
    	myAutoPtr->m_flyCount = 10; // CRASH !!!
    }
    </aircraft>

    Recommendation – unique_ptr does what auto_ptr was intended to do. You should do a search and find on your codebase and replace all auto_ptr with unique_ptr. This is pretty safe but don’t forget to retest your code ! 

    建议 - unique_ptr做什么的auto_ptr是为了做。你应该做一个搜索,并找到你的代码,并更换所有的unique_ptr auto_ptr的。这是相当安全的,但不要忘了重新测试你的代码!

  • 曹纪乾曹浩洋的专栏
  • 曹纪乾曹浩洋的专栏
  • 曹纪乾曹浩洋的专栏
  • 曹纪乾曹浩洋的专栏
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值