views:

1881

answers:

6

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.

+10  A: 

childB.usage = childA.usage;

nos
Much the simplest - possibly faster than memcpy() and certainly no slower.
Jonathan Leffler
@Jonathan, the compiler probably uses memcpy
leiz
Can anyone remember what is the oldest version of C that supports that, rather than having to use memcpy() explicitly?
Craig McQueen
@leiz, I would have guessed that the run-time library is more likely to depend on the compiler than vice versa, i.e. that (instead of what you said) memcpy might be implemented using the same compiler intrinsic as the compiler uses to assign a structure.
ChrisW
@leiz: the compiler probably uses rep movsb, copying structure to structure is built into C, and if it uses memcpy under-the-hood, it is probably by-design(i.e. make the C compiler more portable). @craig: C supports it from the very beginning, you don't need to #include the library memory.h to be able to do a memory copy of value type. likewise, assigning structure to an indirected structure pointer also works.
Michael Buen
http://www.lysator.liu.se/c/c-faq/c-9.html#9-2
Craig McQueen
+8  A: 

Shouldn't it be:

memcpy(&(childB.usage), &(childA.usage), sizeof(rusage))
Alek Davis
The first param in memcpy is destination, so to copy A to B, you need place A as second param.
Alek Davis
And of course, if you have a pointer in the structure, you would need to do some more work to make a duplicate value, but in your example, the structure you want to copy does not have any pointers, so memcpy should be okay.
Alek Davis
+1 - I agree with your logic that since the rusage doesn't have any pointers this should work fine.
James Black
A: 

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.

Am
That will copy the whole child, not "copy just the rusage struct from childA to childB".
ChrisW
you'r right, i misread the end of the question
Am
A: 

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.

Ashwin
A: 

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.

Siva
A: 

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;
}
MarcoC79