Medium
Unbounded recursion with large stack frames
The function below recurses without a base case, and each call frame reserves a 4KB local array, so the stack grows unboundedly. Why does this reliably crash with SIGSEGV, rather than the recursion silently overwriting whatever memory happens to sit past the end of the stack (e.g. heap data or a shared library's mapped code)?
main.cpp
#include <cstdio>
long long recurse(long long n) {
char padding[4096]; // large stack frame per call
padding[0] = (char)n;
return recurse(n + 1) + padding[0];
}
int main() {
printf("%lld\n", recurse(0));
}36 of 53 in Operating Systems