Previous: 14.3 Pointers to Functions
Up: 14 Storage Class and Scope
Next: 14.5 Problems
Previous Page: 14.3.1 Function Pointers as Returned Values
Next Page: 14.5 Problems

14.4 Summary

In this chapter we have discussed the concepts of storage class and scope for variables in a C program. The language provides four storage classes: automatic, register, external, and static. By default, variables declared in functions are of class auto, meaning that memory is allocated for them when the block is entered and automatically deallocated when the block is exited. Such variables may be referenced by name only within the block in which they are declared; i.e. they have local scope. Register storage class, declared with the class specifier, register, are a special case of automatic variables. This class suggests to the compiler that storage for the variable should be allocated in the CPU registers rather than memory. Use of this class should be limited to frequently referenced, time critical variables and only with familiarity with the particular architecture on which the program will be run.

External storage class is used for variables which should remain allocated for the entire execution of a program, and which have global scope. In using external variables, the operation of defining the variable (allocating memory for it) may be independent of declaring the variable (associating a name with the variable). An external variable must be defined exactly once, by specifying its type and name outside any function block. A declaration specified as extern declares the name of the variable without allocating storage, with the expectation that it has been defined elsewhere.

The storage class, static, is used for variables which have local scope, but which remain allocated for the entire program execution. Such variables, while local to a particular function, will retain their values across repeated calls and returns.

We have also seen how memory for variables of different storage classes is allocated in the memory of the computer: automatic variables are allocated and deallocated on the stack, whereas external and static variables are allocated from the heap.

In addition to storage allocated by the compiler, we have seen that additional storage can be allocated dynamically (i.e. at run time) using the malloc() or calloc system functions, and deallocated by the free() function. Data stored in dynamically allocated memory is always referenced indirectly.

Finally, we have expanded our discussion of functions; seeing that they have storage class, like variables; and that we can declare and access function indirectly through pointers. Functions are generally external and have global scope. However, we can limit the scope of a function to be within a single source file by declaring it to be of static storage class.



Previous: 14.3 Pointers to Functions
Up: 14 Storage Class and Scope
Next: 14.5 Problems
Previous Page: 14.3.1 Function Pointers as Returned Values
Next Page: 14.5 Problems

tep@wiliki.eng.hawaii.edu
Sat Sep 3 07:21:51 HST 1994