Recovering from segmentation faults

GermyBoy

Banned
Jun 5, 2001
3,524
0
0
Does anyone know how to do this, or what kind of resources I need to do so?

I am working in ANSI C for a class and have ended up rewriting malloc and free because I wanted to constantly keep track of my memory allocation, deallocation.

Now that that is all ironed out, I wanted to start writing test programs to automatically test my code using random data, garbage or not, and when mine is done, I was planning on testing other peoples with my program is well. However, I want to be able to catch seg faults so that my grading program will be able to recover from them and complete, giving me the score I deserve, not a zero (which is what I would normally score a program that segvs).

Thank you for your time.

Peace,
GermyBoy
 

PCHPlayer

Golden Member
Oct 9, 2001
1,053
0
0
I assume since you are referring to "segmentation faults" that you are running on a Unix box. You can catch a segmentation fault by catching SIGSEGV. Your signal handler can clean up or do whatever you want. Be aware that once you get a segfault all bets are off. You have no guarantees that the signal handler will even finish without another seg fault.
 

GermyBoy

Banned
Jun 5, 2001
3,524
0
0
I figured it out.
But...are there any resources out there for all the types of signals thrown in C? I am very interested in catching them all now, as some are bus errors due to memory allocations, etc.
I know this can be done using things like gdb, but I want to enhance my C skills to the proverbial limit, so I want to learn all the neat tricks.

#include <signal.h>

in the main function

int main()
{
signal(SIGSEGV, error); //catches segmentation faults
signal(SIGABRT, error); //catches aborts (from assert(0) )

return 0;
}

void error()
{
printf("There was a crazy error.\n");
exit(1);
}

Peace,
GermyBoy
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
man signal (might need man 2 signal). man -k signal if I'm wrong, I don't have a Linux box infront of me right now.
 

GermyBoy

Banned
Jun 5, 2001
3,524
0
0
Thanks Nothinman, I figured out what does what. They're really all just integer values anyways, so I could just declare them all until it fails, then I'd figure out how many signals there were. :D

I know what I need to know, no longer needs to be posted in. Lock thread if desired.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
But the values for each signal may vary from platform to platform, if you want semi-portable code at all you really don't want to just use integers and 'hope'.