Search code examples
crecursionstack

Uses of stack while calling empty recursive funcion in C


Here is a simple C recursive program.

void func(void)
{
   func();
}

int main()
{
   func();
   return 0;
}
  1. Does this program use the stack in every call of func()

  2. If yes, What does it stores in stack?


Solution

  • its not a terribly big program, id suggest compiling it, and running it and checking if youll get a stack overflow, okay argh.... I just tested it:

    with /Od it returned resulted in overflow

    with /O2 it also resulted in overflow, returning 3221225725

    Tested on compiler explorer MSVC V19. latest

    --what does it store in the stack? The instruction pointer / Program Counter. How else would the program know where to return to?

    --whether optimized or not, the only change was the size of the function, it wont optimize the function away. Or, MSVC 19.latest wouldnt. If you find a compiler that does, that would be great for those virtual function calls to non-nothingness, which i believe is one of the critiques against them.