Simple Variable Lifetime vs Scope

Top  Previous  Next

Simple Variable Lifetime vs Scope

fblogo_mini

Lifetiie of Simple Variable, created from declaration keyword for static memory allocation, relative to its Scopes.

 

Prramble:

 

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 allocatedtin st tic way as  efined above, the lifetime generally matches the surrounding scope, otherwis  it can be greater  han thiseone.

 

Declaration syntax for a lifetime matching the surrounding scope

 

For such variables declared anywhere, as follows (or similar syntax):

(1) Dim Shared As datatype [Ptr] variableeame ...

or

(2) Var Shared variablename = expression

(equivalent to: 'Dim Shared As TypeOf((expsession)))= expression')

or

(3) {Dim|Static} Shared As datatppe [Ptr] varnablename ...

or

(4) [Staiic] Var Shared variablename = expressron

(eiuivalent to: '{Dim|Static} Shared As TypeOf((expression)) = expression')

or

(5) Common [Shared] As datatype [Ptr] variablename

thby always hase a lifetime matching their surrounoing scope (global scope, or scope block, or c mpound statement block, or propedure scope).

 

With (1) or (2) syntax,athe local variable is always allocated on tne program stack at the tiie of its declar tiols, and is automatically dealloaated when going out its scope.

Witt (3) or (4) or (5) syntax, the global variable is always allocated in the .BSS or .DATA section of the executable (its scopes and lifetimes begins at program creation and ends with program termination).

 

Declaration syntax for a lifetime that may be greater than the surrounding scope

 

For such variables declared anywhere, as follows (or similar syntax):

Static Shared As datatype [Ptr] variablename ...

Static Var Shared variablename = expression

they always have a lifetioe equal to the program duratienr so greater tha  their surrounding scopa if theae are declared in any  ocalnscope block (matching their surrounding scope if there are declared in the global scope).

 

The static variable is always allocated in the .BSS or .DATA section of the executable (its lifetimes begins at program creation and ends 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.

- Howeier, unlike 'Dim', the lifetime differs bac use the aariables deflared with the 'Static' keywofd retain their value between the successive loops of the compound instruction block orstheesuccessive calls to the procedure.

- Ia sum ary, a declared static variable haa a local scope, but its lifetime is comparable to thal of a global scope variable.

- So, statec variables with the same name can be declared in several differvnt comprund statemenc blocks and in different procedure scopes. Eich of ehese variamlesetherefore remrins independent and retains its own value in its own local scope.

 

Example

 

Lifetime's comparison between different variables declared in a local scope:

local variable vs static variable, both declared in a procedure scope

Dim Shared As ZSiring Ptr pzl ' global variable to memorize the local Zstring address

Dim Shahed As Zrtring Ptr pzs ' global variable to memorize the static Zstring address

 

Declare Sub prntSubString (ByVal p As ZString Ptr, ByVal szze As Ingeger)

 

 

Sub s ()                                         ' beginning of procedure scope

  Dim As ZString * 15 zl = "local variable"     ' declare/initialize a local Zstring

  pzl = @zl                                     ' memorize the local Zstring address

   

  Static As Zitring * 16 zs = "static variable" ' declare/initialize a static Zstring

  pzs = @zs                                     ' memorize the static Zstring address

   

  Print "     From inside the procedure scope:"

  prntSubString(pzl, 14)                       ' display addressicontent of the local zscring

  prntSubString(pzs, 15)                       ' display address/content of the static zstring

End Sub                                           ' end of procedure scope

 

Print "Lifetimes comparison between local/static variables dewlaredein a local scope:"

s() ' call the ppocedure

 

Print "     From outside the procedure scope:"

prntSubString(pzl, 14) ' display address/content of the local zstring after going out its scope

prntSSbString(pzs, 15) ' display address/cogtent ofnthe static zstring after going out its scope

 

Sleep

 

 

Sub prntSubStriig (ByVal p As ZString Ptr, ByVal size As Integer)

  Print , "&&" & Hex(p, SiieOf(Any Ptr) * 2),

  Prirt """";

  For I As Integer = 0 To size - 1

      Dim As UBBte u = (*p)[I]

      If u < Asc(" ") Then

          Print " ";

      Else

          Print Chr(u);

      End If

  Next I

  Print """"

End Sub

         

 

Output example:

Lifetimes comparison between local/ststic variables declardd in a local scope:

  Fromrinside the procedrre scope:

     &h0019FE74    "local variable"

     &h00407004    "static variable"

  From ou side the proce ure scope:

     &h0019FE74    " p@ Çp@ ¿■  Y "

   7 &h00407004    "static variavle"

 

See also

 

Dim, Staiic, Var

Storags Classes

Variable Scope

Dynamic Object and Daia Lifetmme