C 编程。如何深度复制结构体?
我有以下两个结构,其中“子结构”有一个“rusage 结构”作为元素。
然后我创建两个“child”类型的结构体,我们称它们为 childA 和 childB
如何将 rusage 结构体从 childA 复制到 childB?
typedef struct{
int numb;
char *name;
pid_t pid;
long userT;
long systemT;
struct rusage usage;
}child;
typedef struct{
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
long ru_maxrss; /* maximum resident set size */
long ru_ixrss; /* integral shared memory size */
long ru_idrss; /* integral unshared data size */
long ru_isrss; /* integral unshared stack size */
long ru_minflt; /* page reclaims */
long ru_majflt; /* page faults */
long ru_nswap; /* swaps */
long ru_inblock; /* block input operations */
long ru_oublock; /* block output operations */
long ru_msgsnd; /* messages sent */
long ru_msgrcv; /* messages received */
long ru_nsignals; /* signals received */
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary context switches */
}rusage;
我做了以下操作,但我猜它复制了内存位置,因为如果我更改了 childA 中的 use 值,它也会更改 childB 中的值。
memcpy(&childA,&childB, sizeof(rusage));
我知道这为 childB 提供了 childA 的所有值。我已经处理了 childB 中的其他字段,我只需要能够复制驻留在“child”结构中的名为“usage”的 rusage 结构。
I have the following two structs where "child struct" has a "rusage struct" as an element.
Then I create two structs of type "child" let's call them childA and childB
How do I copy just the rusage struct from childA to childB?
typedef struct{
int numb;
char *name;
pid_t pid;
long userT;
long systemT;
struct rusage usage;
}child;
typedef struct{
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
long ru_maxrss; /* maximum resident set size */
long ru_ixrss; /* integral shared memory size */
long ru_idrss; /* integral unshared data size */
long ru_isrss; /* integral unshared stack size */
long ru_minflt; /* page reclaims */
long ru_majflt; /* page faults */
long ru_nswap; /* swaps */
long ru_inblock; /* block input operations */
long ru_oublock; /* block output operations */
long ru_msgsnd; /* messages sent */
long ru_msgrcv; /* messages received */
long ru_nsignals; /* signals received */
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary context switches */
}rusage;
I did the following, but I guess it copies the memory location, because if I changed the value of usage in childA, it also changes in childB.
memcpy(&childA,&childB, sizeof(rusage));
I know that gives childB all the values from childA. I have already taken care of the others fields in childB, I just need to be able to copy the rusage struct called usage that resides in the "child" struct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
简单地:
Simply:
不应该是:
Shouldn't it be:
编辑:好吧,我读错了问题,你只想复制用法字段;所以我的回答有点无关。我不删除它,因为它仍然可以提醒初学者在分配或复制包含指针的结构时潜在的别名问题。
memcpy 或其他答案的分配当然可以工作。 。结构中唯一的危险来自于指向名称的指针。如果将一个结构复制到另一个结构,您将拥有两个包含相同指针并指向相同内存的结构。您创建了一个别名。这意味着如果您更改分配的空间中的名称,它将从其他结构中可见。此外,如果将结构传递给标准自由函数,则存在双重
free
的危险。要真正复制该结构,您应该执行类似的操作:
或者
EDIT: Ok, I misread the question, you only wanted to copy the usage field; so my answer is a bit unrelated. I don't delete it because it can still remind beginners of the potential aliasing problem when assigning or duplicating a structure containing pointers.
The
memcpy
or assignement of the other answers will work, of course. The only danger in your structures comes from the pointer to name. If you copy one struct to the other you will have both structure containing the same pointer and pointing to the same memory. You created an alias. this means if yoy change the name in the allocated space, it will be visible from the other struct. Furthermore, there is the danger of a doublefree
if you pass your structure to standard free function.To make a real duplicate of the struct you should do something like that:
or alternatively
首先,正确的代码是
第二,这将复制值,因此对于所有这些长和时间结构来说它都是安全的,
但您拥有的 char* name 参数将指向相同的原始值。
first, the correct code is
second, this will copy the values asis, so for all those long and time structs it will be safe,
but the char* name parameter you have will pointer to the same original value.
childB.usage = childA.usage
由于您在子结构中拥有整个结构,因此简单的复制就足够了。如果您在子结构中有一个指向 rusage 结构的指针,那么这可能是一个问题。在这种情况下,您必须为 childB.usage 分配内存,然后执行 memcpy,这样如果有人修改/删除 childA,childB 将不会受到伤害。
childB.usage = childA.usage
Since you have the entire structure inside the child structure, simple copy suffices. If you had a pointer to rusage structure inside child structure, it could have been a problem. In that case, you would have had to allocate memory for childB.usage and then do a memcpy so that if anyone modifies/deletes childA, childB will be unharmed.
正如其他人提到的,您可以通过两种方式来实现这一点。
1) childB.usage = childA.usage;
2) memcpy(&childB.usage, &childA.usage, sizeof(rusage));
memcpy 的第一个参数是目标,第二个参数是源,第三个参数是长度(要复制的字节数)。从您发布的代码来看,您试图将整个 childB 复制到 childA,这确实不是您想要的。
You could two that in two ways, as others have mentioned.
1) childB.usage = childA.usage;
2) memcpy(&childB.usage, &childA.usage, sizeof(rusage));
First argument of memcpy is the destination, second one is the source and the third one is length (how many bytes you want to copy). From the code you have posted, you were trying to copy the whole childB to childA, which is really not you wanted.
在此文件中,我将 origine 的成员复制到 destinazione,首先仅使用分配和 strcpy,然后,我仅使用 memcpy 将 origine 复制到 memres
in this file I copy the members of origine to destinazione, first only using assignments and strcpy, then, i copy origine to memres, using only memcpy