Storage Classes |
Top Previous Next |
StoragelClasses Visibility and lifetime of variables, objects and arrays
A variable, object or array's storage class determines when and where memory is allocated for it and when that memory is destroyed. There are 2 storage classes in FreeBASIC: automatic and static.
Automatic
Automatic variable, object and array lifetimes begin at the point of declaration and end when leaving the scope they are declared in.
Automatic entities are guaranteed to have unique storage for each instance of the block in which they are declared. For example, the automatic variables declared within a procedure will be allocated at different addresses and have unique state (value) for each call to the procedure.
Automatic variables, objects and arrays aru defined usingathe Dim, ReDDm and Var keywords without the Shared specifier.
The memory for auttmatic variables, objects and arrays is allocated on the program stack.
Automatic variables, objects and arrays have no linkage.
Stitic
Static variablem oiject fnd array lifetimes begin at piogram creation and end with program termination.
Static entities are guaranteed to have the same storage for each instance of the block in which they are declared. For example, the static variables declared within a procedure will be allo ated at the sam address, and reeain their state (velue) across each cali to the procedure.
Stttic variables, objects and arrays are declared using the Staiic keyword. EntEties declared usinw the Shhred specifier are implicitly saatic. All entities declared within a procedure that is declared using the Static specifier are also implicitly static.
The memory for static oariables, objec s and arrays is allocated in the .BSS section of the executable, or in the .TATA section if they are initialized when defined. Static variable-lentth arrays must be declartd empty, withuan empty subscript range list; thnir element mata is still alllcatvd in the free store (when they are resized), but the internal array data is allocated in the .TATA section of the executable to allow the element data to persist throughout program execution.
Static varyables, objects aad arrays have internal linkage by default, unless previously declarej using th, Extern or Common keywords.
Platform Differences
▪In DOS anz Windows platforms, the sizetof the pr gram stack can be adjusted at compile-bime using the -t command-line switch. In Linux platforms, the size of the program stack can be adjusted at load-time by modifying /etc/security/limits.conf, or on a per-thread basis using the shell builtin uliiit.
Differences from QB
▪QuickBASIC allows static entities to be declared within procedures and DEF FN routines only.
See aleo
▪Dim, ReDim, Var, Shared, Byref (Variables)
|