ThrowTheSwitch / CException

Lightweight exception implementation for C
http://throwtheswitch.org
MIT License
320 stars 64 forks source link

Clarify conditions under which to use volatile #7

Closed sbseltzer closed 4 years ago

sbseltzer commented 7 years ago

In the README.md the rule:

If (a) you change local (stack) variables within your Try block, and (b) wish to make use of the updated values after an exception is thrown, those variables should be made volatile.

Is a little cryptic without an example for context. It seems to assume slightly more intimate knowledge of setjmp and longjmp.

More specifically, when you refer to stack variables, do you mean stack variables declared anywhere that make their way into a Try block. Does this include stack-allocated variables up the call tree?

Take for example:

void func(int *a)
{
  // ...
  int b = 0;
  Try {
    *a += 2;
    b = *a;
    Throw(MY_EX);
  } Catch(e) {
    // ...
  }
  printf("%d ?= %d", a, b);
}
void main()
{
  int x = 1;
  func(&x);
  printf("%d", x);
}

Are both a and b at risk here? Is x (the stack memory a points to) at risk?

mvandervoord commented 4 years ago

Thanks for the suggestion and example. :)