//mapping memory in recursive factorial program //jleblanc 2.4.06 //PAC II /* 3) We examined in class a short program that allowed us to show both the values and the locations of variables in a program. Investigate this program (sent separately). Use this program to map out memory and, in particular, explore how "value" and "variable" parameters are implemented ... show both the value and the locations of actual and corresponding formal parameters. Finally, use this approach to map out the use of memory, as much as is possible, in the recursive factorial problem. Submit the revised program, with a brief summary of your results. */ //NOTES /* This Program outputs the value and memory location of the variables used in the recursive factorial program. Bellow I trace the value and memory locations of the variables created by factorial MAIN STEP 1: 1. It shows how the first variable created by the program, first gets a spot in the system stack. 2. The next variable created, answer, is set 4 addresses above first, indicating each variable is alloted A chunk of memory equivilent to 4 memory addresses (which corresponds to 4 bytes?) 3. These variables initially have random junk in them MAIN STEP 2: 1. After the scanf the value of first is changed. RECURSIVE STEPS: 1. The first call to the factorial function (from the main body) creates an instance of n which has the same value as first (since the value first is passed to it). This is located 80 memory addresses above answer. 2. All subsequent calls to factorial create new instance of n that are 88 memory spots above the previous instance of n. This seems to indicate that the activation record for a call to factorial is 88 memory address long, with the value of n at the top of the record. I am not sure why the first instance of n is not 88 memory addresses above answer. MAIN STEP 3: After the value from the result of factorial is passed to answer, answer's value is the result. */ #include int factorial ( int n ) { if (n <= 1) { //End Recursive Step printf(" Recursive step: END \n"); printf(" \t n's value:%9d\t\tmemory location: %u\n", n, &n); return 1; } else { //Intermediate Recursive Step printf(" Recursive step \n"); printf(" \t n's value:%9d\t\tmemory location: %u\n", n, &n); return (n * factorial(n-1)); } } void main() { int first, answer; //STEP 1 printf(" Main Step 1\n"); printf(" \t first's value:%9d\tmemory location: %u\n", first, &first); printf(" \t answer's value:%9d\tmemory location: %u\n\n", answer, &answer); printf(">>please enter the value for factorial: "); scanf("%d", &first); printf("\n"); //STEP 2 printf("\n Main Step 2\n"); printf(" \t first's value:%9d\tmemory location: %u\n", first, &first); printf(" \t answer's value:%9d\tmemory location: %u\n\n", answer, &answer); answer = factorial (first); //STEP 3 printf("\n Main Step 3\n"); printf(" \t first's value:%9d\tmemory location: %u\n", first, &first); printf(" \t answer's value:%9d\tmemory location: %u\n\n", answer, &answer); printf(" \n>>the factorial of %2d is : %6d \n",first, answer); }