[ fromfile: scopestorage.xml id: storageclass ]
Whenever an object is created, space is allocated in one of four possible places. Each of these places is called a storage class.
While scope refers to a region of code where an identifier is accessible, storage class refers to a location in memory.
The static area: global variables, static locals, and static data members are all stored in the static storage area.
The lifetime of a static object begins when its object module loads and ends when the program terminates.
Used often for pointers, simple types, and string constants, less often for complex objects.
The program stack (automatic storage - auto
[59]):
Local variables, function parameters, and temporary objects are all stored on the stack.
For local (block-scope) variables, the lifetime is determined by the brackets around the code that is executed.
Objects on the stack include function parameters, return values, local or temporary variables. Stack storage is allocated automatically when an object definition is executed. Objects in this storage class are local to a function or a block of statements. [60]
The heap or free storage (dynamic storage): objects created via new.
The lifetime of a heap object is determined entirely by the use of new and delete.
In general, the allocation and freeing of heap objects should be in carefully encapsulated classes.
Another storage class, left over from C, is called register. It is a specialized form of automatic storage.
This category of storage can be requested by using the keyword, register in the variable declaration. Most C++ compilers ignore this keyword and put such variables on the stack. Using this storage class for an object means that you cannot take its address.
| Generated: $Date: 2009-09-08 12:15:32 -0400 (Tue, 08 Sep 2009) $ | © 2009 Alan Ezust and Paul Ezust. |