Skip to main content

SA0021: Transporting the address of a temporary variable

Detects address assignments of temporary variables (on the stack) to non-temporary variables

Justification: Local variables of a function or method are created on the stack and they exist only while the function or method is being processed. If a pointer points to this kind of variable after processing the method or function, then you can use this pointer to access undefined memory, or to access an incorrect variable in another function. This situation should be avoided at all costs.

Importance: High

Example 33. Example
FUNCTION TempVarInFUNC : DWORD
VAR
    uiTemp : UINT;
END_VAR
TempVarInFUNC := ADR(uiTemp);    //SA0021
PROGRAM PLC_PRG
VAR
    dwTest : DWORD;
END_VAR
dwTest := TempVarInFUNC();

Output in the Messages view:

  • sa_icon_message.png SA0021: Transporting address of temporary variable to outer scope symbol