title: Error reporting in a C library
body: <p>I am looking for a robust way to report errors in a C library. Consider the simple example of a queue:</p>
  
  <pre><code>struct queue *q = malloc(sizeof(*q));
  if (NULL == q) {
      /* malloc failed. now what ? */
      return NULL; /* maybe ? */
  }
  </code></pre>
  
  <p>Okay, so for that example returning <code>NULL</code> isn't otherwise valid so it makes sense to return it to signal an error. But</p>
  
  <pre><code>void *get_data()
  {
      /* stuff */
  
      /* Error detected. NULL is a valid return, now what ? */
  
      /* stuff */
  }
  </code></pre>
  
  <p>What's more, once we signal an error, how to signal <em>what</em> is the error ? I have thought about it and don't have a satisfying solution.</p>
  
  <ul>
  <li><p>Using <code>errno</code> or some other global object isn't
  something I would like to do (perhaps
  the functions may be called from
  multiple threads etc).</p></li>
  <li><p>I thought of making the client supply
  some "status" object that can be
  inspected after the call, but that
  would make the API quite ugly.</p></li>
  </ul>
  
  <p>So what's your take on the subject ? How do you report errors in a clean way ?</p>