title: How to call ucontext.h getcontext from inside a function
body: <p>I am trying to call getcontext into another function (instead of calling it directly into main) in order to copy the thread's stack and restore it later. This code should print repeatedly, but it won't work once the function that called getcontext returns.</p>
  <p>Is there a way to circumvent this limitation and call getcontext in another function (other than inline macros)?</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;ucontext.h&gt;
  #include &lt;unistd.h&gt;
  
  ucontext_t context;
  
  void set_context() {
      setcontext(&amp;context);
  }
  
  void get_context() {
      getcontext(&amp;context);
  }
  
  int main() {
      get_context();
      puts(&quot;Hello world&quot;);
      sleep(1);
      set_context();
      return 0;
  }
  </code></pre>