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 declare an array size without constant

Anonymous
2016-11-11
2016-11-16
  • Anonymous - 2016-11-11

    Originally created by: barkamharikrishna

    hi all,
    can any one help on Array declaration.
    i am writing an FB. I want to give size of Array with Input variable instead of a constant.

    Example :

    as16_ColorChange : ARRAY [0..100] OF INT;

    instead of 100 value i want give some int variable.

    as16_ColorChange : ARRAY [0..max size] OF INT;

    maxsize is Input var for my FB:

    Many thanks in advance

    Regards
    Harikrishna

     
  • yannickasselin1 - 2016-11-11

    I don't think this is possible. I've been trying to do this for a long time.

    But what you can do is declare your array in the FB like so:

    VAR_IN_OUT
    as16_ColorChange: ARRAY[*] OF INT;
    END_VAR

    And then, you can use LOWER_BOUND and UPPER_BOUND functions to get the lower and upper bounds.

    Ex:
    myLowerBound := LOWER_BOUND(as16_ColorChange, 1);
    myUpperBound := UPPER_BOUND(as16_ColorChange, 1);

    The 1 means first dimension of the array because you can create multiple dimensions variable arrays like so: my3dimArray: ARRAY[,,*] OF INT;

    I hope it helps

     
  • Anonymous - 2016-11-11

    Originally created by: barkamharikrishna

    hi yannickasselin1,
    thanks for the reply
    can please send an example program if you have

    Regards
    Harikrishna

     
  • learnetk - 2016-11-11

    Hi,
    yannickasselin1 is right but,
    i think this Feature is only available in the newest Version of Codesys(Sp9).

     
  • rickj - 2016-11-11

    I think yannickasselin1 has the best answer. The only alternative I know for making a FB that can operate on arrays of different sizes is to pass a pointer to the array and size to the function block as shown below.

       VAR_INPUT
          maxsize         : INT;
          as16_ColorChange   : POINTER TO ARRAY [0..32767] OF INT;
       END_VAR
    

    To use a symbolic constant instead of a literal constant yoy can do the following. However this doesn't help to make a FB that can operate on different size arrays.

       VAR_CONSTANT
          maxsize         : INT:=100;
       END_VAR
       VAR_INPUT
          as16_ColorChange   : ARRAY [0..maxsize] OF INT;
       END_VAR
    
     
  • Anonymous - 2016-11-14

    Originally created by: barkamharikrishna

    hello rjafrate ,
    Can you please give Little more Information please.

    Regards
    Harikrishna

     
  • Anonymous - 2016-11-15

    Originally created by: scott_cunningham

    In general, array size is not dynamic. With the latest CoDeSys releases, you can now dynamically extend memory and arrays as suggested above. Be careful with this - dynamic memory allocation can cause problems - especially when machines are running... In fact, the programming standards offered by the PLCipen group even recommends not doing it at all.

    In your problem, I would either figure out how to not need a variable array size or switch to a POINTER TO BYTE, DataSize and NumberElements for your three inputs. I use this solution for generic ring buffers. However, pointer math now falls on you to get it right.

    Some people (including myself) will tell you to avoid pointers as they are the biggest cause of strange bugs. But casting arrays and pointers to arrays can be just as bad. (This is why I suggested finding a different way that doesn't use an array). Sometimes a diffferent oath simplifies life. Perhaps just burn memory and define the largest array you think you will need.

    None of these are perfect answers and do not show the "flexibility" of a C# or Java or Python or whatever, but we are programming machines, and when code breaks, people fly on Friday nights to fix it!

     
  • Anonymous - 2016-11-16

    Originally created by: barkamharikrishna

    thank you so much

     

Log in to post a comment.