Strings and Memory¶
PLC strings are not dynamic application strings. In PLC programming, memory layout is part of the control system design.
Fixed Memory Matters¶
In many C++ applications, containers such as std::string can grow at runtime by allocating more
memory. PLC logic is different. The PLC cycle must be deterministic: the controller should not
surprise the engineer by allocating memory, moving data, or changing object sizes while the machine
is running.
That means array sizes, DB layouts, UDT layouts, and string capacities are fixed by the compiled PLC program. A value can change at runtime, but the amount of memory reserved for that value does not grow.
This is important because:
- PLC scan time should remain predictable.
- HMI, SCADA, OPC UA, and drive interfaces depend on stable memory layouts.
- Online changes and downloads must know the size of DBs and UDTs.
- Communication partners often expect fixed maximum string lengths.
What A STRING Means¶
In Siemens SCL, a String has a maximum length. The current text may be shorter, but the allocated
capacity is fixed.
VAR
_strAlarmText : String[80];
END_VAR
This reserves space for a string with a maximum content length of 80 characters. Assigning a
shorter text changes the value, not the allocated size.
_strAlarmText := 'Drive not ready';
If the assigned text is too long for the declared capacity, the result depends on the instruction and environment. Treat that as a design error, not as normal behavior.
Arrays Are Fixed Too¶
The same idea applies to arrays.
VAR
_arrxboAlarmActive : Array[1..32] of Bool;
END_VAR
This array always has 32 elements. You cannot append a 33rd element at runtime. If the system can have more alarms later, the interface design must reserve capacity or be changed deliberately.
Practical String Use In PLCs¶
Use strings carefully in cyclic PLC logic. They are useful for diagnostics, HMI display text, test helpers, and simple identifiers. They are usually not ideal for core control decisions.
Prefer numeric codes or enums for machine logic:
inAlarmCode := 1204;
Then let HMI, documentation, or a lookup table translate the code into user-facing text.
Use strings when the text itself is the payload:
strOperatorMessage := 'Start request rejected';
String Checklist¶
- Declare an explicit maximum length when a string crosses an interface.
- Do not assume strings can grow dynamically at runtime.
- Avoid string-heavy logic in fast cyclic control blocks.
- Prefer alarm/status codes for control logic and map them to text at the HMI boundary.
- Reserve array and string capacity deliberately when the interface may grow.