Simple Variable Lifetime vs Scope (IN PROGRESS...)
 
Lifetime of Simple Variable, created from declaration keyword for static memory allocation, relative to its Scopes.

Preamble:
  • The Lifetime of a variable is the time period in which the variable has valid memory (the Scope referring to the program part where its name is visible). The value of a variable may change during its lifetime, but it retains some consistent value.
  • The simple variables considered are the predefined variables including the raw pointers, and the fixed-length strings/arrays, but excluding the variable-length strings/arrays. Instances of simple UDT (without any dynamic data allocated) are also considered.
  • The declaration keywords for static memory allocation are: 'Dim', 'Static', 'Var'.

For such variables allocated in static way as defined above, the lifetime generally matches the surrounding scope, otherwise it can be greater than this one.

Declaration syntax for a lifetime matching the surrounding scope

For such variables declared anywhere, as follows (or similar syntax):
(1) Dim [ByRef] Shared As datatype [Ptr] variablename ...
or
(2) Var [ByRef] Shared variablename = expression
(equivalent to: 'Dim [ByRef] Shared As TypeOf((expression)) = expression')
or
(3) {Dim|Static} [ByRef] Shared As datatype [Ptr] variablename ...
or
(4) Var [ByRef] Shared variablename = expression
(equivalent to: 'Dim [ByRef] Shared As TypeOf((expression)) = expression')
they always have a lifetime matching their surrounding scope (global scope, or scope block, or compound statement block, or procedure scope).

With (1) or (2) syntax ('Shared' not used), these local variables are always allocated on the program stack at the time of their declarations, and are automatically deallocated when going out their scopes.
With (3) or (4) syntax ('Shared' used), these global variables are always allocated in the .BSS or .DATA section of the executable (their scopes and lifetimes begin at program creation and end with program termination).

Declaration syntax for a lifetime greater than (otherwise equal to) the surrounding scope

For such variables declared anywhere, as follows (or similar syntax):
Static [ByRef] Shared As datatype [Ptr] variablename ...
they always have a lifetime equal to the program duration, so greater than their surrounding scope if there are declared in any local scope block (matching their surrounding scope if there are declared in the global scope).

These static variables are always allocated in the .BSS or .DATA section of the executable (their lifetimes begin at program creation and end with program termination).

Interest of declaring such static variables in a compound instruction block or in a procedure scope:
- As for 'Dim', the 'Static' keyword is used in a compound statement block or in a procedure scope to declare variables whose scope stops at the end of the compound statement block or the procedure.
- However, unlike 'Dim', the lifetime differs because the variables declared with the 'Static' keyword retain their value between the successive loops of the compound instruction block or the successive calls to the procedure.
- In summary, a declared static variable has a local scope, but its lifetime is comparable to that of a global scope variable.
- So, static variables with the same name can be declared in several different compound statement blocks and in different procedure scopes. Each of these variables therefore remains independent and retains its own value in its own local scope.

Example



See also