title: Can memset() be called with a null pointer if the size is 0?
body: <p>For one reason or another, I want to hand-roll a zeroing version of <code>malloc()</code>. To minimize algorithmic complexity, I want to write:</p>
  
  <pre><code>void * my_calloc(size_t size)
  {
      return memset(malloc(size), 0, size);
  }
  </code></pre>
  
  <p>Is this well-defined when <code>size == 0</code>? It is fine to call <code>malloc()</code> with a zero size, but that allows it to return a null pointer. Will the subsequent invocation of <code>memset</code> be OK, or is this undefined behaviour and I need to add a conditional <code>if (size)</code>?</p>
  
  <p>I would very much want to avoid redundant conditional checks!</p>
  
  <p>Assume for the moment that <code>malloc()</code> doesn't fail. In reality there'll be a hand-rolled version of <code>malloc()</code> there, too, which will terminate on failure.</p>
  
  <p><sub>Something like this:</p>
  
  <pre><code>void * my_malloc(size_t size)
  {
      void * const p = malloc(size);
      if (p || 0 == size) return p;
      terminate();
  }
  </code></pre>
  
  <p></sub></p>