C ++ 11: Replace all raw pointers with std :: shared_ptr ()?

With the advent of std::unique_ptr damaged std::auto_ptr can be permanently postponed. Therefore, over the past few days, I have been changing my code to use smart pointers and removing all delete from my code.

Although valgrind says my code is clean in memory, the semantic richness of smart pointers will make the code cleaner and cleaner.

In most code, translation is simple: use std::unique_ptr instead of raw pointers held by native objects, throw delete and carefully sprinkle get() , reset() and move() calls, if necessary, interact well with the rest of the code.

I am at the point where I am now translating non-proprietary pointers to smart pointers.

Since I was careful with the lifetimes of my objects (I guarantee that my modules depend in only one direction), valgrind tells me that I have no uninitialized reads, dangling pointers or leaks. So, technically, I could just leave those not owning the source pointers now.

However, one option is to change those that do not own the source pointers to std::shared_ptr , because I know that they are acyclic. Or, would it be better to leave them as source pointers?

I need advice from veteran users of smart pointers regarding what rules you use to decide whether to save non-source pointers as is, or translate them into std::shared_ptr , keeping in mind that I am constantly testing the block and checking my code .

EDIT: Perhaps I am mistaken in using std::shared_ptr - can they be used in combination with std::unique_ptr , or is it so if I use std::shared_ptr , should all descriptors also be std::shared_ptr ?

+47
c ++ memory c ++ 11 smart-pointers
Dec 01 2018-11-11T00:
source share
3 answers

Personally, I do this (more or less):

  • unique_ptrs are for sole ownership
  • raw pointers mean that the one who gave me the raw pointer ensures that the lifetime of this object matches or exceeds my life.
  • shared_ptrs are for sharing.
  • weak_ptrs is when the system wants to check if an object exists before using it. This is rare in my code, since I find it cleaner so that the system guarantees the life of everything that it passed the subsystem (in this case, I use a raw pointer)

So far I have been using more unique_ptrs than shared_ptrs, and more raw pointers than weak pointers.

+87
Dec 04 '11 at 16:12
source share

Use shared_ptr when you need several things to have their own resource (and those who own things can enter and exit the scope "randomly"), use unique_ptr when one thing owns the resource and uses a raw pointer, when you just you need to contact him and not have it (and expect that this referral will not last longer than the resource exists).

There is a fourth type, the type raw-pointer-for- shared_ptr , called weak_ptr . You use this to refer to shared_ptr without actually owning it; you can check if everything is there and use it.

+10
Dec 01 2018-11-11T00: 00Z
source share

The only non-owning smart pointer in the standard library is std::weak_ptr . However, to use it, the actual owner object must hold pointee in std::shared_ptr .

I assume that you used std::unique_ptr for those that were before. If you now convert them to shared_ptr , you will have the advantage that your non-owner pointers can know that the lost owner pointer is a link, while raw pointers can be left dangling, with no chance that a component not belonging to him discovered this, However, shared_ptr will carry (very?) little performance and memory overhead over unique_ptr .

Personally, I recommend using one shared_ptr and a lot of weak_ptr instead of one unique_ptr and a lot of raw pointers in the general case and use unique_ptr if you really have performance problems!

+8
Dec 01 '11 at 10:38
source share



All Articles