Thread: In deep copy

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    Question In deep copy

    I need to do an in deep copy of a struct but I cant have distinct pointers for both my variables... here's some of the code:

    Code:
    TypeEl copieCarte(TypeEl e1, int *err)
    {
    	TypeEl e2;		
    	
    	e2 = initTypeEl(compareCarte, 
    		copieCarte, 
    		getValueCarte,
    		setValueCarte,
    		detruireCarte, 
    		err);
    
    	e2.el = (Carte *)malloc(sizeof(Carte));
    
    	if( e1.copie != copieCarte )  *err = NI;
    	else if( e2.el == NULL )  *err = NEM;
    	else
    	{
    		*err = OK;
    
    		e2.el = e1.el;
    	}
    	
    	return e2;
    }

    The structs referring to that code:

    typedef enum {CHAR, INT, UNSIGNED, LONG, FLOAT, DOUBLE, LDOUBLE, STRING, OTHER} Types;

    typedef struct sTypeEl TypeEl;

    struct sTypeEl

    {
    void *el;

    int (*compare)(TypeEl, TypeEl, int * err);
    struct sTypeEl (*copie)(TypeEl, int *err);
    Types (*getValue)(TypeEl, void *e, int *err);
    Types (*setValue)(TypeEl *,void *e, int *err);
    struct sTypeEl (*detruire)(TypeEl, int *err);

    };

    typedef struct
    {
    Kind kind;
    Value value;
    }Carte;


    Any help/thoughts would be apreciated
    thx

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by Belzebuts
    Code:
    TypeEl copieCarte(TypeEl e1, int *err)
    {
    	TypeEl e2;		
    	
    	e2 = initTypeEl(compareCarte, 
    		copieCarte, 
    		getValueCarte,
    		setValueCarte,
    		detruireCarte, 
    		err);
    
    	e2.el = (Carte *)malloc(sizeof(Carte));
    
    	if( e1.copie != copieCarte )  *err = NI;
    	else if( e2.el == NULL )  *err = NEM;
    	else
    	{
    		*err = OK;
    
    		e2.el = e1.el;  /* pointer copy */
    	}
    	
    	return e2;    /* structure return */
    }
    A thought --

    In the highlighted code, you're replacing the poiinter to the Carte that you just allocated with the pointer contained in the original (e1) instance, thus defeating your deep copy *and* creating a memory leak (the newly allocated Carte is lost).

    You're probably looking to do a structure copy, which can be expressed, in your case, as
    Code:
    *e2.el = *e1.el;  /* structure copy */
    assuming that your compiler supports structure copies. If it does not, you can always use
    Code:
    memcpy(e2.el, e1.el, sizeof(Carte));
    I recommend that you always put a comment in the code someplace that points out that you're doing structure passing, copy, and return. Believe it or not, not all compilers support these features. Also, many experienced C programmers avoid these operations (especially structure passing and structure return) because they can be quite inefficient.
    Insert obnoxious but pithy remark here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LinkedList operator= and deep copy
    By sethjackson in forum C++ Programming
    Replies: 11
    Last Post: 02-28-2008, 12:54 PM
  2. Deep and Shallow Copy
    By peckitt99 in forum C++ Programming
    Replies: 2
    Last Post: 05-13-2007, 07:28 AM
  3. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  4. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 07:43 AM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 11:15 AM