私は「子構造体は、」要素として「のrusage構造体」を持って次の二つの構造体を持っています。Cプログラミング。構造体をディープコピーする方法は?
その後、私はタイプの2つの構造体を作成し、「子供が」私は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));
私が知っているのはchildAのすべての値をchildBに与えていることです。私はすでにchildBの他のフィールドの世話をしました。私はちょうど "子"構造体にあるuseというrusage構造体をコピーできる必要があります。
すべてのご提案の仕事が、私が今までchildAを変更する場合:あなたはそのような何かを行う必要があり、構造体の真の複製を作るために childBも変更されます。 – user69514
コードに問題があります。あなたの質問に対するすべての返答は正しいものであり、あなたが望む効果を生み出します。それがうまくいかない場合、問題は他の場所にあります。 –
@uknown - つまり、2つの構造インスタンスが同じアドレスにある(つまり、同じインスタンスである)か、2つの異なるアドレスでデータの2つの異なるインスタンスが変更されている(つまり、間違っているあなたが報告しているもので)。 – ChrisW