title: Why does malloc() or new never return NULL?
body: <p>I'm writing an application which needs a lot of memory for caching purposes as I described he <a href="https://stackoverflow.com/questions/16641932/dynamical-memory-allocation-making-use-of-unused-memory">here</a>. Now I'm playing around with some malloc / new constructions to figure out how I could realise it. I made a strange observation:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(void) {
    while(1) {
      char *foo = (char*)malloc(1024);// new char[1024];
      if(foo == NULL) { 
        printf("Couldn't alloc\n");
        fflush(stdout);
        return 0;
      }
    }
    return 0;
  }
  </code></pre>
  
  <p>Why does that printf never be reached? If my system runs out of memory, malloc is said to return NULL, as it is explained <a href="http://www.cplusplus.com/reference/cstdlib/malloc/" rel="nofollow noreferrer">here</a>. But I always receive SIGKILL (I'm using linux...).</p>