title: Is it possible to execute code from the stack in standard C?
body: <p>The following code doesn't work as intended but hopefully illustrates my attempt:</p>
  
  <pre><code>long foo (int a, int b) {
    return a + b;
  }
  
  void call_foo_from_stack (void) {
    /* reserve space on the stack to store foo's code */
    char code[sizeof(*foo)];
  
    /* have a pointer to the beginning of the code */
    long (*fooptr)(int, int) = (long (*)(int, int)) code;
  
    /* copy foo's code to the stack */
    memcpy(code, foo, sizeof(*foo));
  
    /* execute foo from the stack */
    fooptr(3, 5);
  }
  </code></pre>
  
  <p>Obviously, <code>sizeof(*foo)</code> doesn't return the size of the code of the <code>foo()</code> function.</p>
  
  <p>I am aware that executing the stack is restricted on some CPUs (or at least if a restriction flag is set). Apart from GCC's nested functions that can eventually be stored on the stack, is there a way to do that in standard C?</p>