I am considering using the setjmp / longjmp based TRY / CATCH macro to handle errors. Otherwise, some of my completely structured functions will be blown up by the ugly if statements and loop flags.
The code is similar to this example:
int trycatchtest(int i)
{
int result = 0;
volatile int error = 100;
volatile uint32_t *var = NULL;
TRY
{
error = 0;
var = os_malloc(4);
*var = 11;
if (i) THROW( i );
}
FINALLY
{
result = *var;
}
END;
return result;
}
THROW is a macro
#define TRY do { jmp_buf buf; switch( setjmp(buf) ) { case 0: while(1) {
#define FINALLY break; } default: {
#define END break; } } } while(0)
#define THROW(x) longjmp(buf, x)
Problem:
When an exception is thrown (for example, I = 1), the var pointer is reset to NULL, although I used the volatile keyword, which should avoid using case for it. From the debugger, I see that it is still in the register, not in memory.
I made a mistake?
EDIT:
I changed the var declaration to
uint32_t * volatile var = NULL;
It works; -)
I don't understand what the difference is:
volatile uint32_t * var = NULL;
means that VALUE is volatile, while the previous declaration makes the pointer volatile?