Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

How to create an array dynamically

erminio79
2009-06-08
2009-06-16
  • erminio79 - 2009-06-08

    Hello everybody,

    it's possible to create an array dynamically?

    I need to create something like this:

    FUNCTION_BLOCK example

    VAR_INPUT

    _UpperBound: INT;
    

    END_VAR

    VAR_OUTPUT

    END_VAR

    VAR

    MyArray :ARRAY[0..UpperBound] OF INT;
    

    END_VAR

    UpperBound :=_UpperBound;


    PROGRAM MAIN

    VAR

         x:INT;
    
         ex:example;
    

    END_VAR

    x := 3;

    ex(_UpperBound := x);


    Does someone can help me ?

    Thanks for you help by advance.

     
  • robkristiaan - 2009-06-12

    It is not possible to create a dynamic array,

    CoDeSys allocates the memory at build time not at runtime.

     
  • spfeif - 2009-06-16

    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.

     

Log in to post a comment.