Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;Boost库-将共享指针传递给函数_C++_Boost_Shared Ptr_Scoped Ptr - Fatal编程技术网

C++ C++;Boost库-将共享指针传递给函数

C++ C++;Boost库-将共享指针传递给函数,c++,boost,shared-ptr,scoped-ptr,C++,Boost,Shared Ptr,Scoped Ptr,假设可以按如下方式创建共享指针 typedef boost::shared_ptr<Employee_t> srdpointer; srdpointer ptr((Employee_t*)malloc(sizeof(Employee_t)),std::ptr_fun(free)); 如何使用shared_ptr实现上述代码中所示的类似功能 你的意思是这样的吗 void allocateBlocks( boost::shared_ptr<int>& ou

假设可以按如下方式创建共享指针

typedef boost::shared_ptr<Employee_t> srdpointer;     
srdpointer ptr((Employee_t*)malloc(sizeof(Employee_t)),std::ptr_fun(free));

如何使用
shared_ptr
实现上述代码中所示的类似功能

你的意思是这样的吗

void allocateBlocks( boost::shared_ptr<int>& out, int& count )
{
  out.reset(new int[...]);
  count = 10;
}
void allocateblock(boost::shared_ptr&out、int&count)
{
out.reset(新的int[…]);
计数=10;
}
PS:我建议看一看boost::shared_array&friends动态分配数组


PS:std::vector也可以吗?

我可以问一下,为什么您使用
malloc
而不是
new
?看起来,std::vector会满足您的需要。您是否知道,
reset()
ing
shared\u ptr
实例和新指针不会改变其他
shared\u ptr
实例(它们将指向旧对象)?“我可以问一下为什么您使用malloc而不是新的”-我们的应用程序是C和CPP代码的混合。-1将
malloc
-ed内存传递到一个结构,该结构将使用
delete
操作符释放它。
void allocateBlocks( boost::shared_ptr<int>& out, int& count )
{
  out.reset(new int[...]);
  count = 10;
}