What's a good safe way to initialise memory for types I don't yet know about?
<p>I started thinking about this after receiving an answer for <a href="http://stackoverflow.com/questions/4060273/why-arent-objects-being-pushed-to-this-vector-when-its-a-member-of-a-struct">this question</a>. This is a bit tricky to explain, but I'll do my best.</p>

<p>I'm building a small(ish) 2D game engine. There are certain requirements that I need to satisfy, since this engine has to "work" with existing code that others have written for a different engine. Some change to existing code is inevitable, but I want to minimise it.</p>

<p>Users of my engine need to define entities called "gadgets". These are basically <code>struct</code>s containing shapes and other state variables. These "gadgets" fall into classes, e.g. they may decide to define an <code>icon</code> gadget or a <code>button</code> gadget - or whatever. </p>

<p>They will also define a message handler for that class of gadgets. </p>

<p>E.g. </p>

<pre><code>typedef struct
{
    shape shapelist[5];
    int num_options;
}interface;

static void interface_message_handler( interface * myself, message * msg )
{
    switch( msg-&gt;type )
    {
        case NEW_MSG:
        {
            interface_descriptor * desc = msg-&gt;desc;

            // initialize myself with contents of this message.
            ...
        }
        break;
    ....
    }
}
</code></pre>

<p>Users have already given me the corresponding message handler function and also the number of bytes in a <code>interface</code> object. And they can then ask the engine to create new instances of their gadgets via IDs e.g:</p>

<pre><code>engine-&gt;CreateNewGadget( interface_gadget_class_ID, welcome_interface_ID );
</code></pre>

<p>where <code>interface_gadget_class_ID</code> is the ID for that class of gadgets and <code>welcome_interface_ID</code> is the instance ID. At some point during <code>CreateNewGadget</code> I need to a) allocate memory to hold a new gadget and then call the gadget class's message handler on it, with a NEW_MSG so that it can initialize itself.</p>

<p>The problem is, if all I'm doing is allocating memory - that memory is uninitialized (and that means all the <code>struct</code> members are uninitialized - so if <code>interface</code> contains a <code>vector</code>, for example, then I'm going to get some wierd results if the message handler does anything with it ).</p>

<p>To avoid wierd results caused by doing stuff to unintialized memory, I really need to call a constructor for that memory as well before passing it to the gadget's message handler function.</p>

<p>e.g in the case of <code>interface</code>:</p>

<pre><code>pfunc(new (memory) interface);
</code></pre>

<p>But my question is, if I have no knowledge of the types that users are creating, how can I do that?</p>
