Using make_shared and allocate_shared in boost?

I can not understand the additional documentation on how to use make_sharedand allocate_sharedto initialize shared arrays and pointers:

shared_ptr<int> p_int(new int); // OK
shared_ptr<int> p_int2 = make_shared<int>(); // OK
shared_ptr<int> p_int3 = allocate_shared(int);  // ??


shared_array<int> sh_arr(new int[30]); // OK
shared_array<int> sh_arr2 = make_shared<int[]>(30); // ??
shared_array<int> sh_arr3 = allocate_shared<int[]>(30); // ??

I am trying to learn the correct syntax to initialize the above variables marked as // ??

0
source share
1 answer

allocate_sharedused the same way make_sharedexcept that you pass the dispenser as the first argument.

boost::shared_ptr<int> p_int(new int);
boost::shared_ptr<int> p_int2 = boost::make_shared<int>();

MyAllocator<int> alloc;
boost::shared_ptr<int> p_int3 = boost::allocate_shared<int>(alloc);

There is no function makefor boost::shared_array, so the only way to do this is to manually allocate memory:

boost::shared_array<int> sh_arr(new int[30]);

boost::make_shared .. - , - boost::shared_ptr:

boost::shared_ptr<int[]> sh_arr2 = boost::make_shared<int[]>(30);
boost::shared_ptr<int[30]> sh_arr3 = boost::make_shared<int[30]>();

, std::shared_ptr .

+3

Source: https://habr.com/ru/post/1667145/


All Articles