C++ 在boost中使用make_shared和allocate_shared?

C++ 在boost中使用make_shared和allocate_shared?,c++,boost,C++,Boost,我无法理解有关如何使用make_shared和allocate_shared初始化共享数组和指针的boost文档: 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>

我无法理解有关如何使用make_shared和allocate_shared初始化共享数组和指针的boost文档:

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); // ??
我正在尝试学习正确的语法来初始化上面注释为//?

的变量allocate\u shared的用法与make\u shared一样,只是将分配器作为第一个参数传递

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);
但是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\u ptr此时不支持数组。

allocate\u shared与make\u shared一样使用,只是您将分配器作为第一个参数传递

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);
但是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目前不支持阵列。

boost::shared_ptr sh_arr2=boost::make_shared30;在这种情况下会正确释放内存吗?现在不支持阵列是什么意思?因为我能够用for循环和[]运算符填充每个元素并访问每个元素…@codekiddy 1如果您使用的是boost 1.53或更高版本,那么内存将正确释放。我说的是标准库中的std::shared_ptr。从C++14开始,它就不支持阵列。一项增加阵列支持的提案被投进了技术规范草案。噢,std::shared_ptr,我现在明白了,谢谢你提供更多信息。非常有用!boost::shared_ptr sh_arr2=boost::make_shared30;在这种情况下会正确释放内存吗?现在不支持阵列是什么意思?因为我能够用for循环和[]运算符填充每个元素并访问每个元素…@codekiddy 1如果您使用的是boost 1.53或更高版本,那么内存将正确释放。我说的是标准库中的std::shared_ptr。从C++14开始,它就不支持阵列。一项增加阵列支持的提案被投进了技术规范草案。噢,std::shared_ptr,我现在明白了,谢谢你提供更多信息。非常有用!