Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.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++_Shared Ptr - Fatal编程技术网

C++ 从原始指针创建共享指针

C++ 从原始指针创建共享指针,c++,shared-ptr,C++,Shared Ptr,我有以下结构: struct Message { size_t m_nBytes; std::tr1::shared_ptr<char> m_msg; //tr1 since I'm not using c++11 Message(char* _message, size_t _size); }; 编写构造函数的正确方法是什么? 我应该使用memcpy/std::copy吗 Message::Message(char* _message, size_t _

我有以下结构:

struct Message
{
    size_t m_nBytes;
    std::tr1::shared_ptr<char> m_msg; //tr1 since I'm not using c++11

    Message(char* _message, size_t _size);
};
编写构造函数的正确方法是什么? 我应该使用memcpy/std::copy吗

Message::Message(char* _message, size_t _size)
: m_nBytes(_size)
, //how to create the shared pointer? 
{
}
谢谢

更新: A.消息不一定包含字符串。 B我正在尝试在c套接字的包装中使用一个结构。因此,我希望: A.包含字符*和大小的结构 B处理结构中的内存分配/释放,并允许用户执行上述行为
C能够从结构中提取字符*和大小,以便在读/发送、写/接收函数中使用它。

为此,我将使用
const char*
constructor并复制文本,因为本地数组已由作用域拥有,其所有权无法转移

Message::Message(char const* _message, size_t _size)
: m_nBytes(_size)
, m_msg(new char[_size], std::default_delete<char[]>())
{
    std::copy(_message, _message + _size, m_msg.get());
}
Message::Message(字符常量*\u Message,大小\u t\u size)
:m_n字节(_大小)
,m_msg(新字符[_size],std::default_delete())
{
std::copy(_message,_message+_size,m_msg.get());
}
如果您想要获得原始指针的所有权,我将使用一个构造函数来表示:

// tells the world you take ownership
Message::Message(std::shared_ptr<char> msg, size_t size)
: m_nBytes(size)
, m_msg(msg)
{
}
//告诉世界你拥有所有权
消息::消息(std::共享消息,大小)
:m_nBytes(大小)
,m_msg(msg)
{
}

注意:因为您指向的是一个数组,所以需要将数组删除器(
std::default_delete()
)传递给
std::shared_ptr
构造函数。

如果对象不打算获得内存的所有权(如您的示例所示),您的对象必须自己分配内存,然后复制源缓冲区。 至少如果使用c++11编译器,您可以使用委托构造函数,并且只需实现一次:

Message::Message(char * ptr, size_t nBytes)
  : m_nBytes(nBytes), m_msg(nBytes?new char[nBytes]:nullptr)
{
  if(ptr)
  {
    assert(nBytes);
    memcpy(m_msg.get(), ptr, nBytes)
  }
}
Message::Message::Message(size_t nBytes)
  : Message(nullptr, m_nBytes)
{}
Message::Message(Message const & other)
  : Message(other.m_msg.get(), other.m_nBytes)
{}

无法从本地分配的内存创建共享指针。这听起来像个XY问题。你到底想要实现什么?你能使用
共享\u ptr
?那么从你现在提出的问题来看:你想要一个自动内存管理的缓冲区,并且可以通过零拷贝操作来传递?A
std::shared_ptr@Brian-为什么不转储指针位并使用<代码> STD::String ?我会考虑使用<代码> STD::vector < /代码>。如果您真的需要共享所有权,那么可能是
std::shared_ptr
?为什么要使用
std::shared_ptr
<代码>标准::字符串
将do@EdHeal
std::string
未共享。我假设这对
OP的
情况很重要。
std::string
是不可变的吗?似乎没有什么意义one@EdHeal我猜不出
OP
想要什么。这个问题特别是关于std::shared_ptrso。。。这就是我要回答的。
shared_ptr
需要c++17,您可以使用
unique_ptr
来代替。。。
Message::Message(char * ptr, size_t nBytes)
  : m_nBytes(nBytes), m_msg(nBytes?new char[nBytes]:nullptr)
{
  if(ptr)
  {
    assert(nBytes);
    memcpy(m_msg.get(), ptr, nBytes)
  }
}
Message::Message::Message(size_t nBytes)
  : Message(nullptr, m_nBytes)
{}
Message::Message(Message const & other)
  : Message(other.m_msg.get(), other.m_nBytes)
{}