Expert C Programming

(Jeff_L) #1

2 if (i>0)


3 a(--i);


4 else


5 printf("i has reached zero ");


6 return;


7 }


8


9 main(){


10 a(1);


11 }


If we compile and run the above program, the flow of control is shown in Figure 6-5. Each dashed box
shows a fragment of source that makes a function call. The statements executed are shown in bold
print. As control passes from one function to another, the new state of the stack is shown underneath.


Execution starts at main, and the stack grows downwards.


Figure Figure6-5. An Activation Record is Created at Runtime for Each Function Call

Compiler-writers will try to speed up programs by not storing information that will not be used. Other
optimizations include keeping information in registers instead of on the stack, not pushing a complete
stack frame for a leaf function (a function that doesn't make any calls itself), and making the callee
responsible for saving registers, rather than the caller. The "pointer to previous frame" within each
frame simplifies the task of popping the stack back to the previous record when the current function
returns.


Programming Challenge

Free download pdf