17-Function-Call-And-Return - CS107 Spring 2007 Handout 17...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS107 Handout 17 Spring 2007 April 25, 2007 Computer Architecture: Take III This handout is compiled from material written by both Nick and Julie. Anatomy of a Function Call In a language that features recursion, it is necessary to distinguish between the definition of a function and a call to that function. The definition of a function defines how that function behaves. A call to the function creates an "instance" or "activation" of that function. Although a function has only one definition, over time it will have many distinct instances. For a recursive function, several instances may exist simultaneously. Each instance has memory requirements. Between the time a function is called and the time it exits, a function's activation record requires memory to contain: local storage parameters, local variables, and any auxiliary space the compiler has determined is necessary to implement that function's block of code. return information where and how execution should continue when this call exits. The memory block allocated for each activation is called an activation record . The memory within an activation record is only valid while the function is executing; once the function exits, the memory is reclaimed. Activation Records An activation record stores the state associated with one instance, or call, of a function. Foremost this requires the activation record to reserve space for the parameters and local variables. Depending on the implementation, the activation record will also store ancillary information associated with the call. The form of the activation record for a function can be deduced at compile time from its declaration. Like a C structure, an activation record is a collection of values each of which has a name and a type. The values are all stored together in a block. The following syntax declares a routine that takes two integers as value parameters: void Simple(int a, int b) // two value params { int temp1, temp2; // temporary local variables .... From the above declaration, the compiler deduces that the activation record will need space for four integers: a , b , temp1 and temp2 . Assuming an integer takes 4 bytes, the activation record will take at least 16 bytes. The compiler does not allocate an activation
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 record now, but later when the function is actually called, the compiler will know to allocate that much space for the new function instance. The Stack A stack is the perfect structure to allocate and de-allocate activations. When a function is activated, space for one of its activation records is pushed onto the stack and initialized. The activation record must remain intact so long as the function executes. When the function exits, the memory for its activation record can be reclaimed. The activation record is popped from the stack and the previous function's activation will become the one on top of the stack. Since function calls are very common, it's important that the stack manage allocation and de-allocation very efficiently. void A(void)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/14/2010 for the course CS 107 taught by Professor Cain,g during the Spring '08 term at Stanford.

Page1 / 6

17-Function-Call-And-Return - CS107 Spring 2007 Handout 17...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online