title: c - properly free memory of a struct
body: <p>My struct named <code>Table</code> holds an array of a struct named <code>Object</code>. Object holds a pointer to another <code>Object</code>. I want to make two methods - one that frees an <code>Object</code> and one that frees a <code>Table</code> when pointers to these structs are given (<code>ObjectP</code> and <code>TableP</code> respectively):</p>
  
  <p>This is my current naive implementation which absolutely doesn't work since valgrid is spitting warnings all over the place (i'm really new to c coming from Java): </p>
  
  <pre><code>/*
   * Represents a linked list containing a key value
   */
  typedef struct Object {
      void *key;
      struct Object *top;
      struct Object *next;
      Boolean originalCell;
  } Object;
  
  /*
   * Represents a table that stores keys based on a given object's hash
   */
  typedef struct Table{
      Object *linkedObjects;
      size_t size, originalSize;
      HashFcn hfun;
      PrintFcn pfun;
      ComparisonFcn fcomp;
      Boolean wasDuplicated;
  } Table;
  
  void FreeObject(ObjectP object)
  {
      free(object);
  }
  
  void FreeTable(TableP table)
  {
      free(table);
  }
  </code></pre>
  
  <p>How should i properly free these structs?</p>
  
  <p>EDIT:</p>
  
  <p>This is how i allocated the variables:</p>
  
  <pre><code>ObjectP CreateObject(void *key)
  {
  struct Object *object = (struct Object*) malloc(sizeof(struct Object));
  ...
  }
  
  TableP CreateTable(size_t tableSize, HashFcn hfun, PrintFcn pfun, ComparisonFcn fcomp)
  {
      struct Table *table = malloc(sizeof(Table));
  
      if (table==NULL)
      {
          ReportError(MEM_OUT);
          return NULL;
      }
  
      table-&gt;linkedObjects = NULL;
      table-&gt;linkedObjects  = malloc(tableSize * sizeof(Object));
  ...
  }
  </code></pre>