4 ms·
> Mercifully, C does not have exceptions This made me smile, because while yes, C just segment faults. I think there’s a way to handle segmentation fault? Or i
by _benj 3y ago
> Mercifully, C does not have exceptions
This made me smile, because while yes, C just segment faults. I think there’s a way to handle segmentation fault? Or is it just the OS that kills the process when it tries to access an invalid memory address?
- topaz0 3y agoYes, at least on linux, the os sends a signal (namely SIGSEGV) to the process, which the process may or may not handle. But kill also sends signals (including SIGSEGV if you specify), so the answer to your question is that it's not really either/or but both. (edit to add: note that it isn't trivial to handle seg fault in particular because unless you do some trickery you return to the same instruction that caused the segfault, and just get another segfault see e.g.: https://stackoverflow.com/questions/2663456/how-to-write-a-signal-handler-to-catch-sigsegv https://stackoverflow.com/questions/2663456/how-to-write-a-s... )
- 01HNNWZ0MV43FF 3y agoSignals are very hard to implement properly, and I imagine signal handlers don't compose well. At some point you'd be better off using `setjmp` and `longjmp` to write your own exceptions from scratch, like Lua does.