Thread: Copying Structures

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    12

    Copying Structures

    I'm having a problem copying structures. As you can see below, MOLECULE contains different types of members, including other structures and strings. I tried making a temporary copy of variable of type MOLECULE simply by doing 'temporary = original;' and the problem is that whenver i change anything in the temporary copy, it changes the original copy. Does anyone know why this happens and how i can fix it? I'm really dreading having to copy everything element by element, but if that's the only way to do it...

    Code:
    typedef struct
      {
        int num; /* some systematic tracking number */
        char *label;
        int species;
        DVEC cart, frac; /* may be useful to keep track of both */
        int symmop; /* the symmetry operator that created this atom */
    //    char type[3]; /* atomic symbol */
    /*    int column[8], residue_nr; only useful for cssr */
        double charge;
        int fftype; /* force field type */
      }
    ATOM;
    
    typedef struct
      {
        char *name; /* may be hard to assign, except for water... */
        int numatoms;
        ATOM *atom;
    //    double total_charge; /* what for? */
        IVEC translation; /* a translation in (abc) may be related to the current position of the molecule */
                          /* what else do we need? */
        DVEC center_mass_cart; /* the centre of mass of the molecule */
        DVEC center_mass_frac; /*the center of mass of the molecule in fractional coordinates */
        DVEC center_geom_cart; /* the centre of geometry of the molecule */
        DVEC center_geom_frac; /* the centre of geometry of the molecule */
        double total_mass; /* total mass of the molecule */
      }
    MOLECULE;

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    There is no reason that should happen. It would more better if you could post us with some more code.

    My sample code donst do anything like that

    Code:
    #include <stdio.h>
    struct node
    {
           int data1;
           int data2;
    };
    
    int main()
    {
        struct node temp1, temp2;
        
        temp1.data1 = 10;
        temp1.data2 = 20;
        
        printf("Temp1\n");
        printf("&#37;d\n",temp1.data1);
        printf("%d\n",temp1.data2);
        
        temp2 = temp1;
        
        temp1.data1 = 30;
        temp1.data2 = 40;
        
        printf("Temp1\n");
        printf("%d\n",temp1.data1);
        printf("%d\n",temp1.data2);
        
        printf("Temp2\n");
        printf("%d\n",temp2.data1);
        printf("%d\n",temp2.data2);
        
        temp2.data1 = 50;
        temp2.data2 = 60;
        
        printf("Temp1\n");
        printf("%d\n",temp1.data1);
        printf("%d\n",temp1.data2);
        
        printf("Temp2\n");
        printf("%d\n",temp2.data1);
        printf("%d\n",temp2.data2);
        
        getchar();
        return 0;
    }
    /*my output
    Temp1
    10
    20
    Temp1
    30
    40
    Temp2
    10
    20
    Temp1
    30
    40
    Temp2
    50
    60
    */
    ssharish2005

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Use of memcpy here..
    Declare an empty Structure to hold the copy of a structure and do something like that:

    memcpy(destination, source, sizeof(the struct you have));

    //Destination and source will be pointers to the structures.
    //If you have an array of structs use the &destination[i], &source[j] for example.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    the problem is that whenver i change anything in the temporary copy, it changes the original copy
    You have a lot of pointers in your structs. So when you copy the molecule struct over, guess what ends up happening -- The new molecule pointers have the same value as the old molecule pointers which means that they're both pointing to the same place in memory. What you're doing is changing the data stored in memory that the pointer is pointing to.

    What you're not realizing is that you're not changing anything in the struct itself as you stated. You're simply dereferencing the pointer in that struct and changing the memory there.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Bokarinho View Post
    Use of memcpy here..
    Declare an empty Structure to hold the copy of a structure and do something like that:

    memcpy(destination, source, sizeof(the struct you have));

    //Destination and source will be pointers to the structures.
    //If you have an array of structs use the &destination[i], &source[j] for example.
    That's not going to fix the OP's problem. There will still be duplicate pointers in both copies of the structs.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying pointers in structures instead of the structure data?
    By Sparrowhawk in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2009, 06:04 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. Classes and Structures.
    By jrahhali in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2004, 05:03 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM