//---------------------------------------------------------------- // signal4.cpp // // This program will attempt to dereference a null pointer, // but beforehand it installs a signal-handler for SIGSEGV. // Now the signal-handler will 'exit' rather than 'return', // but before it exits, it displays a portion of the stack. // // compile-and-link using: $ g++ signal4.cpp -o signal4 // // programmer: ALLAN CRUSE // written on: 20 MAR 2006 //---------------------------------------------------------------- #include // for printf() #include // for signal() #include // for exit() void upon_signal( int signo ) { printf( "\nCaught signal number %d \n\n", signo ); int *tos = &signo; for (int k = 1; k <= 20; k++) { int i = 20 - k; printf( "%08X: %08X =tos[%d] \n", &tos[i], tos[i], i ); } printf( "\n" ); exit( signo ); } int main( int argc, char *argv[] ) { signal( SIGSEGV, upon_signal ); int *ptr = NULL; int x = *ptr; printf( "\nx = %08X \n\n", x ); }