title: SetJmp/LongJmp: Why is this throwing a segfault?
body: <p>The following code summarizes the problem I have at the moment. My current execution flow is as follows and a I'm running in GCC 4.3.</p>
  
  <pre><code>jmp_buf a_buf;
  jmp_buf b_buf;
  
  void b_helper()
  {
      printf("entering b_helper");
      if(setjmp(b_buf) == 0)
      {
          printf("longjmping to a_buf");
          longjmp(a_buf, 1);
      }
      printf("returning from b_helper");
      return; //segfaults right here
  }
  void b()
  {
      b_helper();
  }
  void a()
  {
      printf("setjmping a_buf");
      if(setjmp(a_buf) == 0)
      {
          printf("calling b");
          b();
      }
      printf("longjmping to b_buf");
      longjmp(b_buf, 1);
  }
  int main()
  {
      a();
  }
  </code></pre>
  
  <p>The above execution flow creates a segfault right after the return in b_helper. It's almost as if only the b_helper stack frame is valid, and the stacks below it are erased.</p>
  
  <p>Can anyone explain why this is happening? I'm guessing it's a GCC optimization that's erasing unused stack frames or something.</p>
  
  <p>Thanks.</p>