title: Deallocationg char** inside a struct
body: <p><a href="https://stackoverflow.com/questions/21495330/free-a-char-array-that-is-inside-a-struct-in-c">This</a> problem is similar to mine.<br>
  But deliver no solution for me.</p>
  
  <p>This is only a simplified code for testing and better understanding.<br>
  I know that this code doesn't care of problems after the malloc functions.</p>
  
  <p>The code is for saving words in a struct called List, within a char** storage that used as array.</p>
  
  <p>To create the list and add an item works fine.<br>
  But the deletion of the list makes problems.</p>
  
  <p>Here is the code:</p>
  
  <p>Declaration of the list:</p>
  
  <pre><code>typedef struct {
      char** storage;
  } List;
  </code></pre>
  
  <p>Main:</p>
  
  <pre><code>int main(){
      int size = 2;
      List* list;
      list = new_list(2);
      add(list, "Hello", 0);
      add(list, "World", 1);
      printf("\nlist-&gt;storage[0]: %s", list-&gt;storage[0]);
      printf("\nlist-&gt;storage[1]: %s",  list-&gt;storage[1]);
  
      delete_list(&amp;list,size);
  
      return 0;
  }
  </code></pre>
  
  <p>Make a new list:</p>
  
  <pre><code>List* new_list(size) {
      List* listptr = malloc(sizeof(List));
      listptr-&gt;storage = (char**)malloc(size * sizeof(char));
      return listptr;
  }
  </code></pre>
  
  <p>Add a string to the list:</p>
  
  <pre><code>void add(List* list, char* string, int pos) {
      list-&gt;storage[pos] = (char*)malloc(strlen(string) * sizeof(char));
      list-&gt;storage[pos] = string;
  }
  </code></pre>
  
  <p>Delete the list, with all members:</p>
  
  <pre><code>void delete_list(List** list, int size) {
      int a = 0;
      for (a = 0; a &lt; size; a++)
          free((*list)-&gt;storage[a]);
  
      free((*list)-&gt;storage);
      free(*list);
  }
  </code></pre>
  
  <p>Here I get an error in the for loop, at the line 'free((*list)->storage[a])'.<br>
  The goal is here to delete every allocated string.<br>
  If the list has no members, therefore the code run not in the for loop and the 'delte_list' function work well.</p>
  
  <p>So that is my mistake in the line:
  'free((*list)->storage[a])'</p>