20.3. Storage Class

[ fromfile: scopestorage.xml id: storageclass ]

[Note]Note
  • 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[82]) – Function parameters, local variables, return values, and other temporary objects

    • Objects in this storage class are local to a function or a block of statements.[83]

    • For local (block-scope) variables, the lifetime is determined by the braces around the code that is executed.

    • 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 kept inside carefully encapsulated classes.

    • Another storage class, left over from C, is called register.

    • It is a specialized form of automatic storage that consists of a relatively small quantity of the fastest memory available – usually located on the CPU.

    • 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 but possibly with higher priority for access to register memory.

    • Requesting this storage class for an object means that you cannot take its address with the address-of operator (&).



[82] The optional keyword auto is almost never used.

[83] Or a member of another object that is.