title: 'memdup' function in C?
body: <p>In C, you can use <code>strdup</code> to succinctly allocate a buffer and copy a string into it.  As far as I'm aware, however, there is no similar function for general memory.  For example, I can't say</p>
  
  <pre class="lang-c prettyprint-override"><code>struct myStruct *foo = malloc(sizeof(struct myStruct));
  fill_myStruct(foo);
  
  struct myStruct *bar = memdup(foo, sizeof(struct myStruct));
  // bar is now a reference to a new, appropriately sized block of memory,
  //   the contents of which are the same as the contents of foo
  </code></pre>
  
  <p>My question, then, is threefold: </p>
  
  <ol>
  <li>Is there some standard library function like this that I don't know about?</li>
  <li>If not, is there a succinct and preferably standard way to do this without explicit calls to <code>malloc</code> and <code>memcpy</code>?</li>
  <li>Why does C include <code>strdup</code> but not <code>memdup</code>?</li>
  </ol>