Skip to main content

Attribute: global_init_slot

The pragma defines the order of initialization of POUs and global variable lists: variables within a GVL or a POU are initialized from top to bottom. If there are several global variable lists, then the initialization order is not defined.

The initialization does not apply for the initialization of literal values, for example 1, 'hello', 3.6, or constants of base data types. However, you must define the initialization order yourself if there are dependencies between the lists. You can assign a defined initialization slot to a GVL or POU with the 'global_init_slot' attribute.

Constants are initialized before the variables and in the same order as the variables. During initialization, the POUs are sorted according to the value for <slot>. Then the code for initializing the constants is generated and afterwards the code for initializing the variables.

Syntax:

{attribute 'global_init_slot' := '<slot>'}

Insert location

The pragma always acts on the entire GVL or POU and therefore it must be located above the VAR_GLOBAL or POU declaration.

<slot>

Integer value which defines the position in the order of the calls

The default value for a POU (program, function block) is 50000. The default value for a GVL is 49990. A lower value means an earlier initialization.

Tip

If multiple POUs have been assigned the same value for the 'global_init_slot' attribute, then the order of their initialization remains undefined.

Example 267. Example

The project includes two global variable lists GVL_1 and GVL_2, as well as the PLC_PRG program which uses the variables from both lists. GVL_1 uses the variable B for initializing the variable A, which is initialized in GVL_2 with 1000.

GVL_1

VAR_GLOBAL   //49990
 A : INT := GVL_2.B*100;
END_VAR

GVL_2

VAR_GLOBAL   //49990
 B : INT := 1000;
 C : INT := 10;
END_VAR

PLC_PRG

PROGRAM PLC_PRG  //50000
VAR
 ivar: INT := GVL_1.A;
 ivar2: INT;
END_VAR

ivar:=ivar+1;
ivar2:=GVL_2.C;

In this case, the compiler prints an error because GVL_2.B is used for initializing GVL_1.A before GVL_2 has been initialized. You can prevent this by using the global_init_slot attribute to position GVL_2 before GVL_1 in the initialization sequence.

In this example, GVL_1 must have at least one slot value of 49989 in order to achieve the earliest initialization within the program. Every lower value has the same effect:

GVL_2

{attribute 'global_init_slot' := '100'}
VAR_GLOBAL
  B : INT := 1000;
END_VAR

Using GVL_2.C in the implementation part of PLC_PRG is also not critical even without using a pragma because both GVLs are initialized before the program in either case.



For more information, see: Global Init Slots