Storage Specifiers

Storage class relates to when and where an object is valid.

 

The scope of an object identifies where an object is valid, i.e. its spatial properties

The storage class specifiers (and modifiers) identify when an object is valid, i.e. its temporal properties

 

by keyword:

  • auto
    • Inidcates that the memory location is temporary
    • Only applies to local (block) scoped objects
    • Default for locally (block) scoped objects, and therefore rarely used in practice
  • static
    • Indicates that the memory location is permanent
    • Applies to Globally or Locally scoped objects
    • Object's last value remains in memory, even if the object goes out of scope
  • register
    • Literally a hardware register in the CPU
    • Has no memory location address
    • Used where fast speeds are required
    • Compiler only takes the keyword as a suggestion, not guaranteed
      • Might not use if a register is unavailable
  • extern
    • Refers to an object in an external file
    • external file needs to included at time of compilation
      • e.g. gcc myProg.c extProg.c -o outputFile

 

A couple of small programs to illustrate Storage Class Specifiers:

Compile & Run (g++ myProg.cpp  extProg.cpp  -o outputFile):

Just proving we can call in an external object: 6
Function call 1: Multiplying 0 by 6 = 0
Function call 2: Multiplying 1 by 5 = 5
Function call 3: Multiplying 2 by 4 = 8
Function call 4: Multiplying 3 by 3 = 9
Function call 5: Multiplying 4 by 2 = 8

Leave a Reply