// demo program to show how to capture the segmentation fault signal (SIGSEGV)

#include <signal.h> // for signal()
#include <assert.h> // for assert()
#include <iostream> // for cout, cerr and <<
using namespace std;

void
sigsegv_handler(int signo)
{
    cerr << "this is from sigsegv_handler() ... calling exit(1)" << endl;
    exit(1);
}

int 
main()
{
    // using assert is an easy way to do error checking
    // YOU SHOULD ALWAYS CHECK THE VALUE RETURNED BY A SYSTEM CALL
    assert(signal(SIGSEGV, sigsegv_handler) != SIG_ERR);

    cout << "signal_segv::main() about to cause segmentation fault" 
         << endl << flush;
    // cause a segmentation fault
    int *a = (int *) 42; // don't ever do this
    *a = 86;
}

