Function Call Stack in Action

Now let’s consider how the call stack supports the operation of a square function called by main (lines 9–14 of Fig. 6.13). First the operating system calls main—this pushes an activation record onto the stack (shown in Fig. 6.14). The activation record tells main how to return to the operating system (i.e., transfer to return address R1) and contains the space for main’s automatic variable (i.e., a, which is initialized to 10).

 1   // Fig. 6.13: fig06_13.cpp 2   // square function used to demonstrate the function 3   // call stack and activation records. 4   #include <iostream> 5   using namespace std; 6  7   int square( int ); // prototype for function square 8  9   int main()10 ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.