我正在从客户端服务器通过网络发送一些数据。尽管由于某种原因我无法复制SimpleTestPacket指针,但我正在读取数据包没有任何问题。我尝试使用memset遇到分段错误。码:typedef struct simpleTestPacket_t { uint32_t type; uint8_t point; int32_t value;} SimpleTestPacket;void onReceivePacket(uint8_t header, const char* data, size_t count) { const SimpleTestPacket* packet = reinterpret_cast<const SimpleTestPacket*> (data); SimpleTestPacket* independentPacket = nullptr; memset(independentPacket, packet, sizeof(SimpleTestPacket) * count);}如何将packet指针复制到independentPacket变量,以便将其存储以备后用?是否可以在不分配new内存的情况下进行复制,而我以后将不得不使用delete? (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 只需删除不必要的指针业务,进行本地复制并处理:const SimpleTestPacket* packet = reinterpret_cast<const SimpleTestPacket*> (data);auto independentPacket = *packet;现在independentPacket是带有automatic storage duration的packet的本地副本。 (adsbygoogle = window.adsbygoogle || []).push({});
08-28 05:50