Welcome to the world of compiler optimization!
Due to the as-if rule, the compiler is only required to create something that will have the same observable results as the source code. So, the compiler, if free:
- remove unused arrays
- remove empty loop
- store dynamic arrays from the main external part of the stack - since main is a special function that will be called only once by the environment
If you want to observe stack overflow (bad, not our good site :-)), you should:
- use some code to populate arrays
- compile all remote optimization and preferably in debug mode tell the compiler that I wrote as accurately as you can
The following code does SIGSEGV with CLang 3.4.1 when compiled as cc -g foo.c -o foo
#include <stdio.h> #define SIZE 88388608 void fill(int *arr, size_t size, int val) { for (size_t i=0; i<size; i++) { arr[i] = val; } } int main() { int arr[SIZE]; int arr1[SIZE]; int arr2[SIZE]; fill(arr, SIZE, 0); fill(arr1, SIZE, 0); fill(arr2, SIZE, 0); printf("%d %d %d\n", arr[12], arr1[15], arr2[18]); return 0; }
and even this code works fine when compiling as an optimization level -O2 ... Compilers are now too smart for me, and I am not good enough at the assembly code, which would be the only real way to understand what is actually done!
source share