Thread: Structure Copy "Revisited"

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Structure Copy "Revisited"

    Having already read vjefcoat's post on structure copy i would like (as my first post in this forum) to open a discussion on the issue of structure copying.
    Lest assume we have two structures like this
    Code:
    typedef struct list1{
              int data1;
              int data2;
              struct list1 *next;
              struct list1 *previous;
    } dounlenode; //this is a node for a double linked list
    
    
    typedef struct list2{
              int data1;
              int data2;
              struct list1 *next;
    } singlenode; //this is a node for a sinlgy linked list
    As far as i ve read (i only found it in just one book!) you can copy two structures as simply as:

    Code:
    doublenode dn1,dn2;
    
    dn1=dn2; //this is legal and works like a charm
    I also tried this with pointers

    Code:
    doublenode *dnptr1,*dnptr2;
    
    //...
    //mallocs for the pointers etc.etc.
    //...
    
    *dnptr1=*dnptr2;
    i was also amazed that the following works

    Code:
    doublenode *dnptr1;
    singlenode *sinptr1;
    
    //...
    //mallocs for the pointers etc.etc.
    //...
    
    *dnptr1=*((*doublenode)sinptr1);
    this is a cast of the single list node into a double list node (of which the structure is "bigger") and i copied the contents of the single list node into the double linked node. It worked ok.

    What troubles me are the following:
    1)Is this a good practice?
    2)Is the success of such an assignement machine or compiler dependant?
    3)What happens when the sizes/formats of the structures are different (i think you get a conversion to non scalar type error) Is there a way arround this?

    Your opinions are most welcome. Thank you

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Pokopik View Post
    As far as i ve read (i only found it in just one book!) you can copy two structures as simply as:

    Code:
    doublenode dn1,dn2;
    
    dn1=dn2; //this is legal and works like a charm
    You can indeed copy two identical structures that easily...

    i was also amazed that the following works

    Code:
    doublenode *dnptr1;
    singlenode *sinptr1;
    
    //...
    //mallocs for the pointers etc.etc.
    //...
    
    *dnptr1=*((*doublenode)sinptr1);
    this is a cast of the single list node into a double list node (of which the structure is "bigger") and i copied the contents of the single list node into the double linked node. It worked ok.
    It may not have produced any errors but I seriously doubt it worked as expected. If the smaller struct is copied onto the larger one and the copied portions are the same you may get away with this but it is not a good idea in common practice.

    The underlying action is that one struct is copied to the other. C uses something like memcpy() to do this. It does not invisibly copy variables by name.

    What troubles me are the following:
    1)Is this a good practice?
    2)Is the success of such an assignement machine or compiler dependant?
    3)What happens when the sizes/formats of the structures are different (i think you get a conversion to non scalar type error) Is there a way arround this?
    1) Copying identical structures with the equals sign is just fine and is done all the time.

    2) It should not be compiler dependent.

    3) You should not want a way around copying dissimilar structs onto one another, that's begging for data corruption... In this case you should copy contents piecemeal, not the struct as a whole.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    xian china
    Posts
    31
    Code:
    *dnptr1=*((*doublenode)sinptr1);
    should be
    Code:
    *dnptr1=*((doublenode *)sinptr1);
    Code:
    *dnptr1=*((*doublenode)sinptr1);
    sinptr1 type is "singlenode *", so the memory size to *sinptr1 is the size of singlenode, but if you convert sinptr1 to "doublenode *", if you visit sinptr1->previous, it may generate a segment error

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. Help...accessing character arrays in a structure
    By mathewmc in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 11:20 AM
  3. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  4. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  5. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM