EDIT Before you got excited see important edits in the end and if you're still curious these are reported as:


I have been trying a piece of code and surprised to see that I didn't get a stackoverflow. Trying to simplify things I even got this far:

#include <stdio.h>

int main()
{
    int i;

    /* 1,500,000,000 x 4 bytes = 6,000,000,000 bytes = 6GB */
    int size = 1500000000;
    int arr[size];
    for (i = 0; i < size; i++) {
        arr[i] = 1;
    }
    printf("first: %d\n", arr[0]);
    printf("last:  %d\n", arr[size - 1]);

    return 0;
}

which made me doubt that I don't even know the basics of memory management. I was thinking arr[size] should allocate on stack and overflow easily but instead it it uses all my memory and swap and finishes successfully. What am I missing?

Notes

  • I'm running on 64 bit ubuntu 12.04
  • I have tried with gcc and clang with versions:

    gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
    Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0)
    
  • I have turned the optimizations off (-O0)

  • Running ulimit -a I get:

    core file size          (blocks, -c) 0
    data seg size           (kbytes, -d) unlimited
    scheduling priority             (-e) 0
    file size               (blocks, -f) unlimited
    pending signals                 (-i) 29569
    max locked memory       (kbytes, -l) 64
    max memory size         (kbytes, -m) unlimited
    open files                      (-n) 1024
    pipe size            (512 
Edit
Report