My favorite one is when you write out of bounds to stack allocated array. At that point, you're basically just overwritten random bytes on the stack including return addresses. That was a fun day of debugging
Are at the code section and data section of the memory protected from each other or could you override your own code with random stuff you have in memory?
A lot of begginers struggle with debugging this. Segmentation fault is not specific to C or C++. In fact this is a runtime exception signalled by memory hardware. The compiler is not concerned with this at all, since it's job is to make your instructions executable. Whether some memory access is an access violation or not is only decided by the MMU once the OS tries to execute the specific instruction. As to what went wrong - segmentation fault is always related to memory access (dereferencing pointers, accessing array indices out of bounds, etc). The computer cannot tell you what part of the source code is at fault - after compilation the PC only executes machine code which has little to do with your source and nothing else. To find out what went wrong you can use a debugger such as gdb which keeps track of instruction-to-source mappings. Hope I helped!
Edit: beginners struggle with debugging, not segfaults themselves, everyone has their fair share of segfaults
423
u/Sexy_McSexypants Jan 28 '23
“ok, you wanna tell me what went wrong and where?”
“no, fuck you, Segmentation Fault”