4.1 Storage Allocation

When you define a DEC C variable, the storage class determines not only its scope but also its location and lifetime. The lifetime of a variable is the length of time for which storage is allocated. For OpenVMS systems, storage for a DEC C variable can be allocated in the following locations:

Variables that are placed on the stack or in a register are temporary. For example, variables of the auto and register storage classes are temporary. Their lifetimes are limited to the execution of a single block or function. All declarations of the internal storage classes (auto and register) are also definitions; the compiler generates code to establish storage at this point in the program.

Program sections, or psects, are used for permanent variables; the lifetime of identifiers extends through the course of the entire program. A psect represents an area of virtual memory that has a name, a size, and a series of attributes that describe the intended or permitted usage of that portion of memory. For example, the compiler places variables of the static, external, and global storage classes in psects; you have some control as to which psects contain which identifiers. All declarations of the static storage class are also definitions; the compiler creates the psect at that point in the program. In DEC C, the first declaration of the external storage class is also a definition; the linker initializes the psect at that point in the program.

Table 4-1 shows the location and lifetime of a variable when you use each of the storage-class keywords.

Table 4-1 Location, Lifetime, and the Storage- Class Keywords

Storage Class  Location  Lifetime 
(Internal null)  Stack or register  Temporary 
[auto]  Stack or register  Temporary 
register  Stack or register  Temporary 
static  Psect  Permanent 
extern  Psect  Permanent 
globaldef[1]  Psect  Permanent 
globalref[1]  Psect  Permanent 
globalvalue[1]  No storage allocated  Permanent 

[1] The globaldef, globalref, and globalvalue storage-class specifiers are available only when compiling in VAX C compatibility mode.

For a comparison between the global and external storage classes, see Section 4.3.2.

For more information about psects, see Section 4.8.


Previous Page | Next Page | Table of Contents | Index