C# recursion stack allocation -


void factorial(int n) {  if(n ==0) return 1;  int value = n*factorial(n-1); printf("the value %d", value) } 

assume input function 4.

so number of calls made 5.

i wanted know each time function called, how stack allocation happens. thing below happens

void factorial(4) {  if(4 == 0) return 1;  int value = 4*factorial(3) printf ("the value %d",value);  }  void factorial(3) { if(3 ==0) return 1;  int value = 3* factorial (2);     } 

my question each call, code generated above mentioned in stack }

}

no doesn't generate code, uses same code each call.

read here more information: http://en.wikipedia.org/wiki/call_stack

so there stack pointer points highest point in stack , each function pointer increased on number of bytes needed local variables , system information allocation. , decreases after function call finished.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -