Old said:
You could use setjmp() and longjmp() to detect SIGSEGV and
recover from it. See <setjmp.h> and <signal.h> .
If you're suggesting calling longjmp() from a signal
handler, you're asking for undefined behavior.
7.14.1.1 The signal function
/5/ [...] the behavior is undefined if the signal
handler refers to any object with static storage
duration [with one exception] or if the signal
handler calls any function in the standard library
[with three exceptions].
Since the handler can't safely refer to static-duration
objects, the jmp_buf cannot be a global variable, nor can
a pointer to the jmp_buf be a global, nor a pointer to a
pointer ... Hence, there's no defined way for the handler
to find the jmp_buf and pass it to longjmp().
... and even if it could find the jmp_buf, the handler
still couldn't safely call the longjmp() library function.
(Note that while setjmp() is a macro, longjmp() is described
as an actual function.)
Of course, it would be better to fix the code that causes
the segfault in the first place.
Yes, that's the path to take. As someone has already
observed, there is no reason to believe that the SIGSEGV is
the first thing the program has done wrong, it's just the
first error that was detected. All you know is that the
program went off the rails some time ago, and in light of
the knowledge that the program has run amok there's little
reason to trust anything its memory might now contain.