C to C ++ wrapper, try / catch only
So I have most of the legacy software coded in C. This is for an embedded system, so if something goes wrong how to divide by zero, dereference a null pointer, etc., there isn't much other than a reboot ...
I was wondering if I could only implement main () as C ++ and wrap its contents in try / catch. That way, depending on the type of the thrown exception, I could log some debug information just before rebooting.
Hmm, since there are several processes that might need to wrap each one, not just main (), but I hope you can understand what I mean ...
Is it worth leaving the existing C code (multiple 100 Klocs) untouched, except that it wraps a try / catch around it?
Since this is a built-in application, it probably only runs on one platform.
If so, it is probably much easier and less intrusive to set up a proper interrupt / kernel handler for division by zero and other hard exceptions.
In most cases, this can be done with just a few lines of code. Check if the OS supports such installable exception handlers. If you are on a non-OS system, you can directly wire in processor interrupts that are thrown during exceptions.
source to share
First of all, split by null or null pointer by pointer will not throw exceptions. Fpe exceptions are implemented in the GNU C Library with Signals (SIGFPE). And they are part of the C99 standard, not the C ++ standard.
A few tips based on my experience with embedded C and C ++ development. Depending on your embedded OS:
- Unless you are completely unsure of what you are doing, don't try / catch between threads. Only inside the same thread. Each thread usually has its own stack.
- Throwing in the middle of legacy C code and catching will lead to problems since, for example, when you jumped to a blocking block from the normal flow of the program, the allocated memory cannot be reclaimed, so you have leaks which is much more problematic than division by a zero reversal or a null pointer in an embedded system.
- If you are going to use exceptions, throw them inside your C ++ code, allocating other "C" resources in the constructors and freeing them in the destructors.
- Catch them just above C ++ level.
source to share
I don't think that machine code compiled from C does the same for exceptions, since machine code is compiled for C ++, which "knows" about exceptions. That is, I don't think it is right to expect to catch "exceptions" from C code using C ++ constructs. C does not throw exceptions, and there is nothing to guarantee that errors that occur when C code does something bad are caught by the C ++ exception mechanism.
source to share
Split by zero, dereference null pointers are not C or C ++ exceptions, they are hardware exceptions.
On Windows, you can get hardware exceptions like these wrapped in a regular C ++ exception if _set_se_translator () is used.
__ try / __ catch might also help. Search for __try / __ catch, GetExceptionCode (), _set_se_translator () on MSDN, you can find what you need.
source to share
I think I remember learning how to handle SIGSEGV
(usually generated when resolving an invalid pointer or dividing by 0) and the general advice I found on the net is: do nothing, let the program die. The explanation, I believe to be correct, is that by the time you fall into the SIGSEGV trap, the damaging stream stack is unloaded without repair.
Can anyone comment if this is true?
source to share