title: When should a C function return newly allocated memory?
body: <p>In a response elsewhere, I found the following snippet:</p>
  
  <blockquote>
    <p>In general it is nicer in C to have
    the caller allocate memory, not the
    callee - hence why strcpy is a "nicer"
    function, in my opinion, than strdup.</p>
  </blockquote>
  
  <p>I can see how this is a valid pattern, but why might it be considered nicer? Are there advantages to following this pattern? Or not?</p>
  
  <h3>example</h3>
  
  <p>Recently I've written a fair amount of code that looks something like:</p>
  
  <pre><code>struct foo *a = foo_create();
  // do something with a
  foo_destroy(a);
  </code></pre>
  
  <p>If <code>foo</code> is a anything more than a flat structure, then I figured I could put all my initialization in one step. Also, assume the struct should be on the heap.  Why might it be better form to do something like:</p>
  
  <pre><code>struct foo *a = malloc(sizeof(foo));
  foo_init(a);
  // do something with a
  foo_destroy(a)
  </code></pre>