title: Segmentation fault after swap context
body: <p>Why do I get segmentation fault when executing the following piece of code? I noticed that if I declare a second stack ( char s2[10000]; and init_context2(&amp;unew2, s2, 10000);) everything works fine. But I can't understand why two stacks are required. Is it possible to use only one, if yes how?</p>
  
  <p>output:
  before context init
  before context swap
  Message
  Message2
  Segmentation fault</p>
  
  <pre><code>#define _GNU_SOURCE
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ucontext.h&gt;    
  ucontext_t uold, unew, unew2;
  char s1[10000];
  
  static void message()
  {
      puts("Message");
  }
  
  static void message2()
  {
      puts("Message2");
  }
  
  void init_context(ucontext_t * uc, void* stack, size_t stack_size)
  {
      //puts("Inside init context");
      getcontext(uc);
      uc-&gt;uc_link = &amp;uold;
      uc-&gt;uc_stack.ss_sp = stack;
      uc-&gt;uc_stack.ss_size = stack_size;
      uc-&gt;uc_stack.ss_flags = 0;
      makecontext(uc, message, 0);
  }
  
  void init_context2(ucontext_t * uc, void* stack, size_t stack_size)
  {
      //puts("Inside init context 2");
      getcontext(uc);
      uc-&gt;uc_link = &amp;uold;
      uc-&gt;uc_stack.ss_sp = stack;
      uc-&gt;uc_stack.ss_size = stack_size;
      uc-&gt;uc_stack.ss_flags = 0;
      makecontext(uc, message2, 0);
  }
  
  int main(int argc, char** argv) {
  
      int i;
      for(i=0; i&lt;10; i++)
      {
          puts("before context init");
          init_context(&amp;unew, s1, 10000);
      init_context2(&amp;unew2, s1, 10000);
      puts("before context swap");
      swapcontext(&amp;uold, &amp;unew);
      swapcontext(&amp;uold, &amp;unew2);
      }
  
      return (EXIT_SUCCESS);
  }
  </code></pre>