The cost of passing by shared_ptr

Always pass your shared_ptr by const reference: void f(const shared_ptr<Dataset const>& pds) {…} void g(const shared_ptr<Dataset const>& pds) {…} Edit: Regarding the safety issues mentioned by others: When using shared_ptr heavily throughout an application, passing by value will take up a tremendous amount of time (I’ve seen it go 50+%). Use const T& instead of … Read more

Getting a normal ptr from boost::shared_ptr?

Use the get() method: boost::shared_ptr<foo> foo_ptr(new foo()); foo *raw_foo = foo_ptr.get(); c_library_function(raw_foo); Make sure that your shared_ptr doesn’t go out of scope before the library function is done with it — otherwise badness could result, since the library may try to do something with the pointer after it’s been deleted. Be especially careful if the … Read more

shared_from_this causing bad_weak_ptr

John Zwinck’s essential analysis is spot on: The bug is that you’re using shared_from_this() on an object which has no shared_ptr pointing to it. This violates a precondition of shared_from_this(), namely that at least one shared_ptr must already have been created (and still exist) pointing to this. However, his advice seems completely beside the point … Read more

Should I switch from using boost::shared_ptr to std::shared_ptr?

There are a couple of reasons to switch over to std::shared_ptr: You remove a dependency on Boost. Debuggers. Depending on your compiler and debugger, the debugger may be “smart” about std::shared_ptr and show the pointed to object directly, where it wouldn’t for say, boost’s implementation. At least in Visual Studio, std::shared_ptr looks like a plain … Read more

std::shared_ptr initialization: make_shared() vs shared_ptr(new Foo) [duplicate]

Both examples are rather more verbose than necessary: std::shared_ptr<int> p(new int); // or ‘=shared_ptr<int>(new int)’ if you insist auto p = std::make_shared<int>(); // or ‘std::shared_ptr<int> p’ if you insist What’s the difference? The main difference is that the first requires two memory allocations: one for the managed object (new int), and one for the reference … Read more

Why does unique_ptr take two template parameters when shared_ptr only takes one?

If you provide the deleter as template argument (as in unique_ptr) it is part of the type and you don’t need to store anything additional in the objects of this type. If deleter is passed as constructor’s argument (as in shared_ptr) you need to store it in the object. This is the cost of additional … Read more

Why can’t a weak_ptr be constructed from a unique_ptr?

If you think about it, a weak_ptr must refer to something other than the object itself. That’s because the object can cease to exist (when there are no more strong pointers to it) and the weak_ptr still has to refer to something that contains the information that the object no longer exists. With a shared_ptr, … Read more

Example to use shared_ptr?

Using a vector of shared_ptr removes the possibility of leaking memory because you forgot to walk the vector and call delete on each element. Let’s walk through a slightly modified version of the example line-by-line. typedef boost::shared_ptr<gate> gate_ptr; Create an alias for the shared pointer type. This avoids the ugliness in the C++ language that … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)