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.
Use debuggers to diagnose segfaults Start your debugger with the command gdb core , and then use the backtrace command to see where the program was when it crashed. This simple trick will allow you to focus on that part of the code.
A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system).
No, memory leaks by themselves would not cause a segmentation fault. However, memory leaks usually indicate sloppy code, and in sloppy code other issues, which would cause a segmentation fault, are likely to be present.
What Is Segmentation Fault? In a nutshell, segmentation fault refers to errors due to a process's attempts to access memory regions that it shouldn't. When the kernel detects odd memory access behaviors, it terminates the process issuing a segmentation violation signal (SIGSEGV).
Expanding on my comment...
There's two possibilities I can think of:
The compiler is optimizing out the entire buf
array:
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:
sub rsp, 20971520
won't segfault. It's just a pointer. It will only segfault when you try to access it into unmapped memory.
Declaring your buf
array involves nothing more than incrementing the stack pointer.
Incrementing the stack pointer itself beyond the limit of the stack area is not going to make the program crash - it's just a register that happens to have a larger value. Referring to memory beyond that area, however, will most certainly make the program crash.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With