Concepts_of_Programming_Languages by Unknown

Concepts_of_Programming_Languages by Unknown

Author:Unknown
Language: eng
Format: epub


This section discusses the issues related to variables that are defined within sub-programs. The issue of nested subprogram definitions is also briefly covered.

9.4.1 Local Variables

Subprograms can define their own variables, thereby defining local referencing environments. Variables that are defined inside subprograms are called local variables , because their scope is usually the body of the subprogram in which they are defined.

400 Chapter 9 Subprograms

In the terminology of Chapter 5, local variables can be either static or stack dynamic. If local variables are stack dynamic, they are bound to storage when the subprogram begins execution and are unbound from storage when that execution terminates. There are several advantages of stack- dynamic local variables, the primary one being flexibility. It is essential that recursive subprograms have stack- dynamic local variables. Another advantage of stack- dynamic locals is that the storage for local variables in an active subprogram can be shared with the local variables in all inactive subprograms. This is not as important an advantage as it was when computers had smaller memories.

The main disadvantages of stack- dynamic local variables are the following: First, there is the cost of the time required to allocate, initialize (when neces-sary), and deallocate such variables for each call to the subprogram. Second, accesses to stack- dynamic local variables must be indirect, whereas accesses to static variables can be direct. 4 This indirectness is required because the place in the stack where a particular local variable will reside can be determined only during execution (see Chapter 10). Finally, when all local variables are stack dynamic, subprograms cannot be history sensitive; that is, they cannot retain data values of local variables between calls. It is sometimes convenient to be able to write history- sensitive subprograms. A common example of a need for a history- sensitive subprogram is one whose task is to generate pseudoran-dom numbers. Each call to such a subprogram computes one pseudorandom number, using the last one it computed. It must, therefore, store the last one in a static local variable. Coroutines and the subprograms used in iterator loop constructs (discussed in Chapter 8) are other examples of subprograms that need to be history sensitive.

The primary advantage of static local variables over stack- dynamic local variables is that they are slightly more efficient— they require no run- time over-head for allocation and deallocation. Also, if accessed directly, these accesses are obviously more efficient. And, of course, they allow subprograms to be history sensitive. The greatest disadvantage of static local variables is their inability to support recursion. Also, their storage cannot be shared with the local variables of other inactive subprograms.

In most contemporary languages, local variables in a subprogram are by default stack dynamic. In C and C++ functions, locals are stack dynamic unless specifically declared to be static . For example, in the following C (or C++) function, the variable sum is static and count is stack dynamic.

int adder( int list[], int listlen) {

static int sum = 0;

int count;

for (count = 0; count < listlen; count ++)

sum += list [count];

return sum;

9.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.