Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shared_ptr and slicing

Tags:

c++

shared-ptr

Someone I worked with once said that shared_ptr was unsafe and would slice when casting from a derived class to a base class (i.e. upcasting). For example if there were 2 classes A and B where B derived from A, then

shared_ptr<A> a(new B)

would slice. I pointed him to http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/shared_ptr.htm where it says

shared_ptr<T> can be implicitly converted to shared_ptr<U> whenever T* can be implicitly converted to U*.

implied that it's safe to use in those contexts but he didn't seem to think so.

like image 291
sashang Avatar asked Jul 22 '10 04:07

sashang


People also ask

What is the use of shared_ptr?

shared_ptr is also helpful in C++ Standard Library containers when you're using algorithms that copy elements. You can wrap elements in a shared_ptr , and then copy it into other containers with the understanding that the underlying memory is valid as long as you need it, and no longer.

What is shared_ptr?

std::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer. Several shared_ptr objects may own the same object.

What is the difference between shared_ptr and unique_ptr?

Use unique_ptr when you want to have single ownership(Exclusive) of the resource. Only one unique_ptr can point to one resource. Since there can be one unique_ptr for single resource its not possible to copy one unique_ptr to another. A shared_ptr is a container for raw pointers.

What is the difference between shared_ptr and Weak_ptr?

The only difference between weak_ptr and shared_ptr is that the weak_ptr allows the reference counter object to be kept after the actual object was freed. As a result, if you keep a lot of shared_ptr in a std::set the actual objects will occupy a lot of memory if they are big enough.


1 Answers

That someone is wrong, object slicing doesn't apply to pointers. That the pointer usage is wrapped away in a shared_ptr doesn't change that - it doesn't do any particular magic here, it initializes an internal pointer with the value passed to its constructor.

Simplified it could look e.g. like this for the purpose of this question:

template<class T> struct ptr {
    T* t;
    ptr(T* t) : t(t) {}
    // ...
};

You lose the static type-information of B, yes, but nothing changes regarding the object pointed to.

like image 162
Georg Fritzsche Avatar answered Sep 27 '22 20:09

Georg Fritzsche