C++结构体数组复制
我想使用 memcpy 将结构体数组的元素复制到另一个结构体数组。我相信这会导致我的程序因某种原因失败。另外我到底如何释放内存?
struct FaultCodes
{
string troubleFound;
string causeCode;
string actionCode;
string paymentCode;
string suppCode;
u_int16_t multiplier;
};
struct JobFaultInfo
{
static const size_t NUM_CODES = 5;
FaultCodes codes[NUM_CODES];
};
FaultCodes codes[JobFaultInfo::NUM_CODES];
// I have populated codes with the data.
JobFaultInfo info;
memcpy(info.codes, codes, sizeof(FaultCodes)*JobFaultInfo::NUM_CODES);
I want to copy elements of a struct array to another by using memcpy. I believe this is miserably causing my program to fail for some reason. Also how can I free the memory in the end ?
struct FaultCodes
{
string troubleFound;
string causeCode;
string actionCode;
string paymentCode;
string suppCode;
u_int16_t multiplier;
};
struct JobFaultInfo
{
static const size_t NUM_CODES = 5;
FaultCodes codes[NUM_CODES];
};
FaultCodes codes[JobFaultInfo::NUM_CODES];
// I have populated codes with the data.
JobFaultInfo info;
memcpy(info.codes, codes, sizeof(FaultCodes)*JobFaultInfo::NUM_CODES);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅当您复制的对象是所谓的 POD(普通旧数据结构)时才允许使用 memcpy。但是您的结构包含 std::string 对象,它们不是 POD。因此,您的整个结构不是 POD。
请改用算法头中的 std::copy 。
You are only allowed to use memcpy in case the objects you're copying are so-called PODs (plain old data structures). But your struct contains std::string objects which are not PODs. Thus, your whole struct is not a POD.
Use std::copy from the algorithm header instead.
std::string
通常包含一个指向存储字符的位置的指针。通过对每个字符串使用 memcpy,您将获得两个 std::string 实例,它们都认为必须释放该内存。砰。您可以使用
中的std::copy
来代替。但更好的是,使用 std::vector 而不是原始数组。
干杯&嗯。
A
std::string
usually contains a pointer to where it stores the characters. By usingmemcpy
for each string you get twostd::string
instances who both think they have to deallocate that memory. Bang.You can use
std::copy
from<algorithm>
instead.But even better, use a
std::vector
instead of a raw array.Cheers & hth.
一旦进入std::域,就不能使用memcpy。这是因为包括 string 在内的所有 std 容器都将数据存储在其他位置,并且只保留该数据的指针。因此,如果您只是进行按位复制(如在 memcpy 中),您完全不知道您复制了什么。
正如建议的那样,不要使用普通数组和 memcpy 分别使用向量和 std::copy 。这些不会像 memcpy 那样具有快速复制的好处,但它比前一种方法可靠得多。
如果您仍然想坚持使用 memcpy 那么正如我所解释的,您不能使用 string。在您的结构中,您将拥有静态(堆栈)内存的二维字符数组。聊天 [5][MAX_STRING_LEN]。结构中的所有字符串都必须映射到该数组的每个元素中。现在您的结构将由纯数据组成,没有指针,因此您可以使用 memcpy。
但请不要这样做,只需 将向量与 std::copy 一起使用。
The moment you enter into the std:: domain, you can not use memcpy. That is because all the std containers including string store the data in other location and only keep the pointers of that data to themselves. So if you simply do bitwise copy (as in memcpy) you exactly don't know what you have copied.
As suggested instead of using the plain arrays and memcpy use vectors and std::copy respectively. These will not have the benefit of fast copying as memcpy does but it is much more reliable than the former method.
If you still want to stick with memcpy then as I explained you can not use string. In you structure you will have two dimensional char array of static (stack) memory. chat [5][MAX_STRING_LEN]. All the strings in your structures must be mapped into each of the elements of this array. Now your structure will consist of pure data and no pointer, and hence you can use memcpy.
But please don't do this, simply use vector with std::copy.