1

Just a quick query regarding shared_ptr:

so i have this code:

std::shared_ptr<int> asd(new int[10]);

From my understanding, i am making a single shared_ptr<int> that points to an array of 10 integers. if my understading is correct then here are my questions:

1.) How do i access the values/initialize ?

2.) Is it possible to use make_shared?

3.) What if i want to make an array of 10 shared_ptr and make them point to a single int?

I am also aware that i need to supply a custom deleter when i deal with arrays using shared_ptr. Using vectors will also make it easier.

3
  • I would also like to know how to access the values. Commented Jun 4, 2015 at 21:14
  • This isn't really possible in C++ without shenanigans. See stackoverflow.com/questions/8947579/… Commented Jun 4, 2015 at 21:24
  • I don't think this is really a dupe. It doesn't ask whether you should use or not a shared_ptr, but how to use it properly. Commented Jun 4, 2015 at 21:41

1 Answer 1

4
  1. This is a very very bad idea. You are creating indeed an array of 10 ints, but the default deleter uses delete instead of delete[] to deallocate the memory. You either have to specify a custom deleter,

    std::shared_ptr<int> asd(new int[10]{0}, [](int* _p)
    {
        delete[] _p;
    }); // also zero-initializes
    

    or use

    std::unique_ptr<int[]> asd(new int[10]{0}); // no need for a deleter here
    

    instead, as unique_ptr has an array specialization, and the deleter uses the proper delete[] for this specialization. The examples above zero-initialize the dynamically allocated arrays. If you want other values, then dereference the pointers and set the values, as you'd do in a raw pointer situation. To access the value in the array pointed by a shared_ptr, you cannot use arr[j] syntax anymore, as shared_ptr does not overload operator[]. You need to get the managed raw pointer and use it like

    *(asd.get()+2) // this dereference the third element
    

    or

    asd.get()[2] // same as above
    

    As you can see, shared_ptr is not really made for arrays. On the other hand, unique_ptr overloads operator[] for its managed array, so you can simply use asd[2] to refer to the third element in the array.

  2. You cannot use make_shared, as make_shared does not allow you to specify the deleter.

  3. Simply create an array of 10 shared_ptr from an initial shared_ptr that points to your desired int

    std::shared_ptr<int> sp(new int{42});
    std::array<std::shared_ptr<int>, 10> arr_sp;
    for(auto& elem: arr_sp)
        elem = sp;
    

    DO NOT construct all elements of the array from the same raw pointer, as you end up with multiple shared_ptrs managing the same raw pointer, which is always a bad idea.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for a precise answer but with regards to question#1, how do i access the shared_ptr's value if ever? because asd[0] gives me error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.