title: Unit testing for failed malloc()
body: <p>What is the best way for unit testing code paths involving a failed <code>malloc()</code>? In most instances, it probably doesn't matter because you're doing something like </p>
  
  <pre><code>thingy *my_thingy = malloc(sizeof(thingy));
  if (my_thingy == NULL) {
    fprintf(stderr, "We're so screwed!\n");
    exit(EXIT_FAILURE);
  } 
  </code></pre>
  
  <p>but in some instances you have choices other than dying, because you've allocated some extra stuff for caching or whatever, and you can reclaim that memory. </p>
  
  <p>However, in those instances where you can try to recover from a failed <code>malloc()</code> that you're doing something tricky and error prone in a code path that's pretty unusual, making testing especially important. How do you actually go about doing this?  </p>