Skip to content

Commit 1bb001c

Browse files
committed
Merge remote-tracking branch 'origin/master' into embecosm-online-chane
2 parents 051c9ed + 014a437 commit 1bb001c

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

book/src/variables.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,61 @@ PROGRAM PLC_PRG
5757
...
5858
END_PROGRAM
5959
```
60+
61+
### Pointer Initialization
62+
63+
A pointer variable can be initialized with the address of a global reference or an IEC-address using the `AT` or `REFERENCE TO` syntax. `REF_TO` pointers can be initialized using the built-in `REF` function in its initializer.
64+
65+
This initialization, however, does not take place during compile time. Instead, each pointer initialized with an address will be zero-initialized to a null pointer by default. The compiler collects all pointer initializations during compilation and creates internal initializer functions for each POU. These functions are then called in a single overarching project-initialization function, which can be called either manually in your main function or by a runtime. Additionally, global variables — whether they are initialized pointers or POU instances containing pointer initializers — are also assigned within this overarching function.
66+
67+
This function follows a naming scheme (`__init___<project name>`) that varies slightly depending on whether a build config (`plc.json`) was used.
68+
69+
- **When using a build config (`plc.json`)**, the project name is used:
70+
71+
_Build config snippet:_
72+
```json
73+
{
74+
"name": "myProject",
75+
"files": []
76+
}
77+
```
78+
_Resulting symbol:_
79+
```iecst
80+
__init___myProject()
81+
```
82+
83+
- **When compiling without a build config**, the name of the first file passed via CLI is used as the base for the name.
84+
85+
_CLI command:_
86+
```bash
87+
# build command
88+
plc myFile1.st myFile2.st
89+
```
90+
_Resulting symbol:_
91+
```iecst
92+
__init___myFile1_st()
93+
```
94+
95+
It is important to note that if there are pointer initializations present in your project, failing to call the initialization function in your runtime or in `main` will result in **null pointer dereferences** at runtime.
96+
97+
### Example
98+
_myProject.st_:
99+
```iecst
100+
VAR_GLOBAL
101+
myGlobal : STRING;
102+
END_VAR
103+
104+
PROGRAM prog
105+
VAR
106+
myString : REF_TO STRING := REF(myGlobal);
107+
myOtherString : REFERENCE TO STRING REF= myGlobal;
108+
myAlias AT myGlobal: STRING;
109+
myAnalogSignal AT %IX1.0 : REAL;
110+
END_VAR
111+
// ...
112+
END_PROGRAM
113+
114+
FUNCTION main: DINT
115+
__init___myProject_st();
116+
prog();
117+
END_FUNCTION

0 commit comments

Comments
 (0)