我有以下两个结构,其中“子结构”有一个“使用结构”作为元素。
然后我创建了两个“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 中的使用值,它也会改变 childB。
memcpy(&childA,&childB, sizeof(rusage));
我知道这给了 childB 来自 childA 的所有值。我已经处理了 childB 中的其他字段,我只需要能够复制位于“child”结构中的名为 usage 的 rusage 结构。