|
CS 3723/3721 Programming Languages Spring 2005 Recitation 8 Runtime Storage Week 8: Mar 7-11 Due (on time): 2005-03-21 23:59:59 Due (late): 2005-03-25 23:59:59 |
Recitation 8 must be submitted
following directions at: submissions with deadlines
|
This recitation is short and will count for only half the credit of numbers 1-7.
C Program To Print The Run-Time Stack | Output of the C program |
---|---|
% cat stack_linux.c /* stack_linux.c: investigate run-time stack */ #include <stdio.h> void top_of_stack() { unsigned long int i = 999999999; unsigned long int *p = &i; unsigned long int count = 0; while (*p != 111111111 && count < 100) { printf("%i: %u, \t%u\n", count, *p, (unsigned long int)p); p++; count++; } printf("%i: %i\n", count, *p); } int factorial(int n) { unsigned long int i = 333333333; unsigned long int j = n + 110000000; unsigned long int ret = 220000000; printf("Enter factorial, n=%i\n", n); if (n <= 1) { top_of_stack(); return 1; } ret = n*factorial(n-1); printf("Leave factorial, n=%i\n", n); return ret; } int call_factorial(int n) { unsigned long int i = 111111111; return factorial(n); } int main() { printf("Factorial of 4 = %i\n", call_factorial(4)); } % cc -o stack_linux stack_linux.c % stack_linux > stack_linux_output % cat stack_linux_output ADDRESSES OF FUNCTIONS printf: 134513256 top_of_stack: 134513448 factorial: 134513562 call_factorial: 134513689 main: 134513718 | Enter factorial, n=4 Enter factorial, n=3 Enter factorial, n=2 Enter factorial, n=1 NUM STACK CONTENT STACK ADDRESS 0: 999999999, 3221217588 1: 3221217624----------+ 3221217592 2: 134513623 | 3221217596 3: 1108544768 | 3221217600 4: 134514094 | 3221217604 5: 3221217640--------+ | 3221217608 6: 220000000 | | 3221217612 7: 110000001 | | 3221217616 8: 333333333 | | 3221217620 9: 3221217672------+ | +--3221217624 10: 134513645 | | 3221217628 11: 1 | | 3221217632 12: 2 | | 3221217636 13: 3221217672------+ +----3221217640 14: 1107621074 | 3221217644 15: 1108544768 | 3221217648 16: 134514094 | 3221217652 17: 3221217688------|---+ 3221217656 18: 220000000 | | 3221217660 19: 110000002 | | 3221217664 20: 333333333 | | 3221217668 21: 3221217720----+ +------3221217672 22: 134513645 | | 3221217676 23: 2 | | 3221217680 24: 3 | | 3221217684 25: 3221217720----+ +--3221217688 26: 1107621074 | 3221217692 27: 1108544768 | 3221217696 28: 134514094 | 3221217700 29: 3221217736----|-----+ 3221217704 30: 220000000 | | 3221217708 31: 110000003 | | 3221217712 32: 333333333 | | 3221217716 33: 3221217768--+ +--------3221217720 34: 134513645 | | 3221217724 35: 3 | | 3221217728 36: 4 | | 3221217732 37: 3221217924--|--+ +--3221217736 38: 3221217796--|----+ 3221217740 39: 1073791680 | | 3221217744 40: 134518584 | | 3221217748 41: 1107579949 | | 3221217752 42: 220000000 | | 3221217756 43: 110000004 | | 3221217760 44: 333333333 | | 3221217764 45: 3221217800-++----------3221217768 46: 134513713 | 3221217772 47: 4 | 3221217776 48: 134514205 | 3221217780 49: 3221217816---+ | 3221217784 50: 1107384036 | 3221217788 51: 1107621024 | 3221217792 52: 111111111 +-----3221217796 Leave factorial, n=2 Leave factorial, n=3 Leave factorial, n=4 Factorial of 4 = 24 |
The output of the program is in three columns: first a counter value, second the value in memory, and third the memory address of that value. The output has been edited in several ways, including especially drawing lines linking some values under "STACK CONTENT" with values under "STACK ADDRESS", and cleaning up the output in other ways. The headers at the top of the output were not printed by the program. Also the function addresses on the left (titled "ADDRESSES OF FUNCTIONS" and starting "printf: 134513256") were printed by a different program. This output is possible because C does not restrict the addresses of a pointer variable. This output would not be possible in Java.
Contents of submission
for Recitation 8: Last Name, First Name; Course Number; Recitation Number. a. Describe the point of execution represented by the 52 numbers printed at the right in the table above. What functions are in a state of execution as these 52 numbers are printed? b. In the code on the left, several whole assignment statements and one value assigned have been colored orange in the C code above. What is special about this code? Do these assignments and their values have anything to do with calculating the factorial? Why do you think this code is there? c. In the output on the right, various numbers are colored orange. What are these numbers doing there? d. In the output on the right, various numbers are colored dark green. What are these number there for? What is the meaning of the dashed lines I have drawn between columns 2 and 3? e. In the output on the right, various numbers are colored blue. What are these number there for? f. In the output on the right, various numbers are colored red. What are these number there for? (I don't know what the black numbers are, or what their purpose is.)
|