title: How to limit available memory to make `malloc()` fail?
body: <p>I'd like to make <code>malloc()</code> fail by limiting the memory available.</p>
  
  <pre><code>$ ulimit -v 1000
  $ ./main.exe 10000000
  0x102bfb000
  </code></pre>
  
  <p>But even with ulimit, the following program still finishes correctly. Does anybody know how to make <code>malloc()</code> fail? Thanks.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(int argc, char *argv[]) {
      size_t size = atoi(argv[1]);
      void *ptr = NULL;
  
      if((ptr = malloc(size)) == NULL) {
          perror("malloc()");
          exit(1);
      }
  
      printf("%p\n", ptr);
      free(ptr);
      return 0;
  }
  </code></pre>
  
  <p>EDIT: The above is on Mac OS X.</p>
  
  <p>On Linux, I got segmentation fault. Why <code>malloc()</code> can cause segmentation fault? How to make <code>malloc()</code> return a NULL pointer?</p>