• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Recovering from segmentation faults

GermyBoy

Banned
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
 
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.
 
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
 
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.
 
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. 😀

I know what I need to know, no longer needs to be posted in. Lock thread if desired.
 
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'.
 
Back
Top