title: How to properly delete an object that is a void pointer?
body: <p>I'm trying to interface a C++ class (e.g., <code>class foo</code>) to C. What I have done so far is to define a C structure that holds an opaque pointer member variable (i.e., <code>void*</code>), that points to the associated C++ <code>foo</code> object.</p>
  <pre><code>struct C_foo {
    void *foo_obj;
  };
  </code></pre>
  <p>I defined an <code>alloc()</code> C interface function that allocates objects of type <code>C_foo</code>:</p>
  <pre><code>struct C_foo* alloc(/* input */) {
    struct C_foo *out = new struct C_foo;
    out-&gt;foo_obj      = new foo(/* input */);
  
    return out;
  };
  </code></pre>
  <p>What I want to do now, is to create a <code>dealloc()</code> C interface function that will properly deallocate objects of type <code>C_foo</code> previously allocated with <code>alloc()</code> shown above:</p>
  <pre><code>void dealloc(struct C_foo *obj) {
    /* ??? */
  }
  </code></pre>
  <p>I know that explicitly deleting the <code>void*</code> pointer (i.e., <code>delete obj-&gt;foo_obj;</code>) would results in undefined behaviour (<em>§ 5.3.5/1 [expr.delete]</em>):</p>
  <blockquote>
  <p><em>The <code>delete</code>-expression’s result has type <code>void</code> [81].</em></p>
  <p><em>[81] This implies that an object cannot be deleted using a pointer of type</em> <em><code>void*</code></em> <em>because <code>void</code> is not an object type</em>.</p>
  </blockquote>
  <h2>Question:</h2>
  <p>How I'm going to properly deallocate a <code>struct C_foo</code> object?</p>