title: C - Storing pointers with malloc() in an array, can't free() them afterwards
body: <p>I want to store <code>pointers</code> that have been allocated using <code>malloc()</code> in an <code>array</code> and then <code>free</code> all of them after. However even though the program doesn't complain it doesn't work. Below <code>cleanMemManager()</code> won't actually <code>free</code> the memory as when tested inside <code>main()</code> the char* <code>pointer</code> is not <code>NULL</code> and it will print <code>???</code>.</p>
  
  <p><strong>code:</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  void **ptrList = NULL;
  
  void tfree(void** ptr)
  {
      free(*ptr);
      *ptr = NULL;
  }
  
  void* talloc(int size)
  {
      void* ptr = malloc(size);
      ptrList[0] = ptr;   ///No clue if this actually does what I think it does
      return ptrList[0];
  }
  
  void initMemManager()
  {
      ptrList = (void**)malloc(sizeof(void**) * 3);
      memset(ptrList, 0, sizeof(void**) * 3);
  }
  
  void cleanMemManager()
  {
      tfree(&amp;ptrList[0]); //Doesn't free the right pointer it seems
  }
  
  int main()
  {
      initMemManager();
      char* ptr = (char*)talloc(3);
      cleanMemManager();
  
      if (ptr != NULL) //This will trigger and I'm not expecting it to
          printf("???");
  
      getchar();
      return 0;
  }
  </code></pre>
  
  <p>I don't understand the syntax to use for this, does the pointer not actually get touched at all? What is it freeing then since it doesn't throw any errors?</p>