title: HOW TO: Deep Copy of Linked List
body: <p>I am having some issues with a program I am writing in C, and I have surpassed my knowledge. In summary, I need to deep copy a link list from one list to another. The lists have malloc'd data in them and I need to preserve all the data without having pointers pointing at the same information.</p>
  
  <p>I have only posted the code that I think is relevant. If I am missing any important contextual information please let me know.</p>
  
  <p>Here is the code:</p>
  
  <h3>matrix.h</h3>
  
  <pre><code>typedef struct matrix {
          char *name;
          int R;
          int C;
          int dim;
          void (*concat_matrices)( struct matrix *A, struct matrix *B, struct matrix *ret );
  } Matrix;
  
  void concat_matrices( Matrix *A, Matrix *B, Matrix *ret ) {
          int L1 = strlen( A-&gt;name );
          int L2 = strlen( B-&gt;name );
          int len = L1 + L2;
          char *Ap = (char*)malloc(L1*sizeof(char)); strcpy(Ap,A-&gt;name);
          char *Bp = (char*)malloc(L2*sizeof(char )); strcpy(Bp,B-&gt;name);
          char *c = (char*)malloc(sizeof(char)*(len + 2));
          c[0] = '('; strcat(c, Ap); strcat(c, Bp); c[len+1] = ')';
          ret-&gt;name = (char*)malloc(sizeof(char)*(len + 2));
          strcpy(ret-&gt;name, c);
          ret-&gt;R = A-&gt;R; ret-&gt;C = B-&gt;C;
          ret-&gt;dim = ret-&gt;R*ret-&gt;C;
          free(Ap); free(Bp); free(c);
  }
  </code></pre>
  
  <h3>matrix_list.h</h3>
  
  <pre><code>typedef struct node {
          Matrix *M;
          struct node *next;
          struct node *prev;
  } Node;
  
  typedef struct matrix_list {
          Node *head;
          Node *tail;
          int size;
          void (*append)( struct matrix_list *list, Matrix *M );
          void (*print)( struct matrix_list *list );
          void (*reverse_print)( struct matrix_list *list );
          void (*delete)( struct matrix_list *list, const char *name );
          void (*delete_tail)( struct matrix_list *list );
          void (*delete_head)( struct matrix_list *list );
          void (*release)( struct matrix_list *list );
          void (*clone_list)( struct matrix_list *from, struct matrix_list *to );
  } MatrixList;
  
  ...
  
  void clone_list( MatrixList *from, MatrixList *to ) {
          if( from-&gt;head == NULL ) {
                  to = NULL;
          } else {
                  Node *tmp = from-&gt;head;
                  while( tmp != NULL ) {
                          Matrix *m_copy = (Matrix*)malloc(sizeof(Matrix*));
                          char *c_copy = (char*)malloc(sizeof(char*)*strlen(tmp-&gt;M-&gt;name));
  
                          strcpy(c_copy,tmp-&gt;M-&gt;name);
                          m_copy-&gt;name=c_copy;
                          m_copy-&gt;R=tmp-&gt;M-&gt;R;
                          m_copy-&gt;C=tmp-&gt;M-&gt;C;
                          m_copy-&gt;concat_matrices = concat_matrices;
  
                          to-&gt;append( to,m_copy );
                          tmp = tmp-&gt;next;
                  }
          }
  }
  </code></pre>
  
  <h3>main.c</h3>
  
  <pre><code>chain-&gt;print(chain);
  
  MatrixList *chain_copy = (MatrixList*)malloc(sizeof(MatrixList));
  set_list_functions( chain_copy );
  chain-&gt;clone_list(chain, chain_copy);
  chain_copy-&gt;print(chain_copy);
  </code></pre>
  
  <p>The problem arises when I try and print out the clone. Because I am malloc'ing in the clone function I understand the data goes out of scope. How can I do this copy so after the function is called clone has its own version of the data?</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>First I would like to thank you all for taking your time to answer my question, and for teaching me more about C. I have only been coding for about 3 years. And I have a lot to learn. The updated source with 0 errors from Valgrind can be found at.</p>
  
  <p><a href="http://matthewh.me/Scripts/c++/matrix_chain/" rel="nofollow">http://matthewh.me/Scripts/c++/matrix_chain/</a> for anyone else trying to figure out things like me. User = guest Password = guest. The clone_list function now looks like this.</p>
  
  <pre><code>void clone_list( MatrixList *from, MatrixList *to ) {
          if( from-&gt;head == NULL ) {
                  to = NULL;
          } else {
                  Node *tmp = from-&gt;head;
                  while( tmp != NULL ) {
                          Matrix *m_copy = (Matrix*)malloc(sizeof(Matrix));
                          m_copy-&gt;name = (char*)malloc(strlen(tmp-&gt;M-&gt;name) + 1);
  
                          sprintf( m_copy-&gt;name, "%s", tmp-&gt;M-&gt;name );
                          m_copy-&gt;R=tmp-&gt;M-&gt;R;
                          m_copy-&gt;C=tmp-&gt;M-&gt;C;
                          m_copy-&gt;concat_matrices = concat_matrices;
  
                          to-&gt;append( to,m_copy );
                          tmp = tmp-&gt;next;
                  }
          }
  }
  </code></pre>
  
  <p>If anyone else sees anything else wrong and would like to add additional pointers please feel free to do so.</p>