user69514
user69514

Reputation: 27629

C Programming. How to deep copy a struct?

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.

Upvotes: 9

Views: 37263

Answers (7)

Patrick Schlüter
Patrick Schlüter

Reputation: 11841

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 double free if you pass your structure to standard free function. To make a real duplicate of the struct you should do something like that:

memcpy(&childA,&childB, sizeof(rusage));    
if(childB.name)
  childA.name = strdup(childB.name);

or alternatively

childA = childB;
if(childB.name)
  childA.name = strdup(childB.name);

Upvotes: 3

nos
nos

Reputation: 229108

Simply:

childB.usage = childA.usage;

Upvotes: 21

MarcoC79
MarcoC79

Reputation: 1

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct inner
{
    char *parola;
    int n;
} interna;

typedef struct outer
{
    struct inner *ptr;
    int numeroesterno;
} esterna;


struct another
{
    struct inner *ptr;
    int numero;
};    //never forget ; here

int main(void)
{
    esterna *origine; //ptr to structs
    struct another *destinazione;
    struct another *memres;

    char *tmpParola;
    tmpParola = malloc(30*sizeof(char));
    strcpy(tmpParola, "AAAAA");

    interna *tmp;  //remember the TYPEDEF, and don't use struct interna
    tmp = (interna *)malloc(sizeof(struct inner));
    // if you use struct interna in sizeof you get
    //  error: invalid application of ‘sizeof’ to incomplete type ‘struct interna’ 

    tmp->n = 500;
    tmp->parola = tmpParola;

    origine = (esterna *)malloc(sizeof(struct outer));

    origine->numeroesterno = 2;
    origine->ptr = tmp;  //the data structer pointed by tmp has already been allocated and set

    // now I have the structure allocated and set, I want to copy this on destinazione
    destinazione = (struct another *)malloc(sizeof(struct another));

    destinazione->numero = origine->numeroesterno;

    //destinazione->ptr = tmp;  //in this case you don't copy struct inner, it's just a reference

    destinazione->ptr = (interna *)malloc(sizeof(struct inner));
    destinazione->ptr->parola = malloc(sizeof(char)*30);
    strcpy(destinazione->ptr->parola, origine->ptr->parola);
    destinazione->ptr->n = 111;

    //modify origine

    origine->numeroesterno = 9999;
    strcpy(origine->ptr->parola, "parola modificata in origine");

    //print destinazione

    printf("\nparola in destinazione :%s\n", destinazione->ptr->parola);
    printf("\nparola in origine :%s\n", origine->ptr->parola);

    //you can see that destinazione is a copy, because mofifying origine, destinazione deosn't change

    //now we play with memcpy

    memres = (struct another *)malloc(sizeof(struct another));

    memcpy(memres, destinazione, sizeof(destinazione)); //till here, is AAAAA
    strcpy(destinazione->ptr->parola, "parola modificata in destinazione");

    printf("\nmemcpy, numero %d\n", memres->numero);
    printf("\nmemcpy, parola :%s\n", memres->ptr->parola);

    //as you can see from the output, memcpy doesn't make a copy of destinazione:
    //modifying destinazione->ptr->parola after the assignment affects what memres carries with it
    //So from the idea that I got, memcpy just creates the pointers to the originary structure

    free(origine->ptr->parola);
    free(origine->ptr);
    return 0;
}

Upvotes: 0

Siva
Siva

Reputation: 721

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.

Upvotes: 0

Ashwin
Ashwin

Reputation: 3637

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.

Upvotes: 0

Alek Davis
Alek Davis

Reputation: 10732

Shouldn't it be:

memcpy(&(childB.usage), &(childA.usage), sizeof(rusage))

Upvotes: 10

Amirshk
Amirshk

Reputation: 8258

first, the correct code is

memcpy(&childA,&childB, sizeof(child));

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.

Upvotes: 0

Related Questions