Robkristiaan is correct but if you are trying to change an upper bound "globally" like inside functions you build in a library ect... You can use a global constant like:
VAR_GLOBAL CONSTANT
MAX_ALARM_INDEX :INT := 24;
END_VAR
then you can change it in one place in the global variables and use it like:
MyArray :ARRAY[0..MAX_ALARM_INDEX] OF INT;
Make sure you select (Check box) in the PROJECT-OPTIONS-BUILD menu "Replace Constants" other wise you will get a compile error.
This is the closest to getting a dynamic array allocation by replacing with a constant.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello everybody,
it's possible to create an array dynamically?
I need to create something like this:
FUNCTION_BLOCK example
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR
END_VAR
UpperBound :=_UpperBound;
PROGRAM MAIN
VAR
END_VAR
x := 3;
ex(_UpperBound := x);
Does someone can help me ?
Thanks for you help by advance.
It is not possible to create a dynamic array,
CoDeSys allocates the memory at build time not at runtime.
Robkristiaan is correct but if you are trying to change an upper bound "globally" like inside functions you build in a library ect... You can use a global constant like:
VAR_GLOBAL CONSTANT
MAX_ALARM_INDEX :INT := 24;
END_VAR
then you can change it in one place in the global variables and use it like:
MyArray :ARRAY[0..MAX_ALARM_INDEX] OF INT;
Make sure you select (Check box) in the PROJECT-OPTIONS-BUILD menu "Replace Constants" other wise you will get a compile error.
This is the closest to getting a dynamic array allocation by replacing with a constant.