0

I have been trying to dynamically allocate a shared pointer in C++. However, I keep getting the errors

/usr/include/boost/make_shared.hpp:15:0: In installation of 'class boost::detail::sp_ms_deleter<char []>':
In file included from /usr/include/boost/make_shared.hpp:15:0,
       from main.cpp:10:
/usr.include/boost/smart_ptr/make_shared.hpp:140:64:    required from 'boost::shared_ptr<X> boost::make_shared(Args&& ... ) [with T = char []; Args = { char*} ]'
/usr/include/smart_ptr/make_shared.hpp:41:48: error: invalid application of 'sizeof' to incomplete type 'char []'

boost::shared_ptr<char[]> A = boost::make_shared<char[]>(50);

Using std::shared_ptr I get similar errors.

I need a way to allocate a dynamic array without using the new keywords as these are project requirements, so another method of doing that could be a valid solution for me as well.

  • You can't use `shared_ptr` with arrays, **but** you can use `unique_ptr` to achieve so. Is `shared_ptr` a necessity? And is so `boost`? How about simply `auto c_ptr = std::make_unique(50);`? To my knowledge the `shared_ptr` to plain arrays will be added in C++20 – Fureeish Dec 20 '17 at 15:28
  • Since C++17 shared pointers have the [`operator[]`](http://en.cppreference.com/w/cpp/memory/shared_ptr/operator_at). – Ron Dec 20 '17 at 15:28
  • @Ron `auto ptr = std::make_shared(50);` fails to compile with `-std=c++17` on my compiler – Fureeish Dec 20 '17 at 15:30
  • Why char[] and not a std::vector ? – M.L. Dec 20 '17 at 15:31
  • What version of boost are you using? New versions support allocating an array using `boost::make_shared` (see [this](http://www.boost.org/doc/libs/1_66_0/libs/smart_ptr/doc/html/smart_ptr.html#make_shared)). `std::make_shared` will also gain this ability in C++20. – Praetorian Dec 20 '17 at 15:34
  • 1
    @Fureeish Using `make_shared` - no, using the constructor - yes. [Relevant reading](http://open-std.org/JTC1/SC22/WG21/docs/papers/2017/p0674r0.html). – Ron Dec 20 '17 at 15:35
  • @Ron thank you for clarifying – Fureeish Dec 20 '17 at 15:37

0 Answers0