Variable storage and scope

Local (auto) variables

Local variables are recognized only in the function or macro where they are declared. You cannot simply access a variable declared in one function from another function. You can use the same variable name in different functions but it will not refer to the same variable.

 

Declaration example:

int i

 

Function parameters also are local variables.

 

Local variables exist only as long as the function is executing. They are automatically created when entering the function and destroyed when leaving the function. Every executing function instance has its own set of local variables.

 

Local variables are located on the stack, in order they were declared. They are aligned on 4 bytes (variables of size 1 to 4 occupy 4 bytes, variables of size 5 to 8 occupy 8 bytes, etc). Variables of composite types (str, BSTR, ARRAY, VARIANT, interface pointers) can also have associated data in dynamic memory. Memory allocation is automatic.

Global (static) variables

Global variables are recognized in all functions and macros (only inside QM, of course). Use global variables to share data between different macros or between different instances of the same macro. Where possible, use local variables.

 

Declaration example:

int+ i

 

Global variables are created when compiling (functions and macros are automatically compiled when running first time or after text has changed) the function or macro where they are declared, and destroyed when QM exits or other macro list file opened. Global variables are located in dynamic memory.

 

The same global variable can be declared multiple times in multiple places. If it is initialized in the declaration statement, the assignment operation is performed each time.

Application variables

Local variables of the main function of a folder that has "Application" property are recognized in other functions of that application.

Member variables and functions

Members of a class (an user-defined type that has member functions) are recognized in all member functions of that class. That is, instead of using syntax variable.member, use just member or this.member.

Thread variables (shared)

Thread scope is intermediate between local and global. Thread variables declared with "-" are shared between all functions of the same thread (running macro). They must be declared in each function where used.

 

Declaration example:

int- i

 

Thread variables are created at run time, at the beginning of the function where first time declared in the thread. They are destroyed when the thread exits (macro ends).

 

If a thread variable is initialized in the declaration statement, the assignment is performed only first time during thread execution (differently than global variables). Example:

 

int- i=5 ;;even if this line is encountered sevel times during thread execution, 5 is assigned only the first time

 

See also: thread variables in special threads

Thread variables (private)

Variables declared with "--" are thread variables that are visible only in that function.

 

Declaration example:

int-- i

Scope priority

If there are two or more visible variables with the same name and different scope, is used variable that is above in this list:

  1. Predefined (you cannot define variables with the same name as one of predefined variables).
  2. Local, thread (you cannot define local and thread variables with the same name in the same function).
  3. Member.
  4. Application.
  5. Global.

 


See also: more types of storage, predefined variables