So, I do:
$ ulimit -s
8192
Great. As I understand this, the stack segment of any process cannot exceed 8192 kilobytes.
Now, to challenge that..
#include <stdio.h>
void over_8k(void) {
char buf[1024*1024*20];
}
int main(int argc, char** argv) {
printf("Starting .. ");
over_8k();
printf(" finishing.\nHow did this work?\n");
return 0;
}
Compiled. Ran. No problems. Well this isn’t right? over_8k alone should have a stack frame of, well, over 20 megabytes. Well, let’s try accessing those 20 million bytes:
#include <stdio.h>
#include <string.h>
void over_8k(void) {
char buf[1024*1024*20];
memset(buf, 'A', sizeof(buf));
}
int main(int argc, char** argv) {
printf("Starting .. ");
over_8k();
printf(" finishing.\nHow did this work?\n");
return 0;
}
.. drum roll ..
Segmentation fault: 11
Great. But that’s not the error I’d expect? Invalid memory access?
Why does it raise a segfault, and doesn’t error out earlier? On call to over_8k perhaps? How does this work? I want to know everything.
Expanding on my comment…
There’s two possibilities I can think of:
The compiler is optimizing out the entire
bufarray:In MSVC, with optimizations enabled, the entire array is being completely optimized out and is not allocated at all. So it’s not using any stack.
Stack allocation is just an increment/decrement to the stack pointer:
won’t segfault. It’s just a pointer. It will only segfault when you try to access it into unmapped memory.