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

Is there a 'deepcopy' type function in Codesys STL?

SimonD
2022-10-06
2022-10-06
  • SimonD - 2022-10-06

    As most of you know, in this line of code:
    a := b
    Codesys does NOT copy the value of b to a, it creates a link between b & a that remains active until you overwrite it with something else. This can be extremely useful for lots of applications but it can also be a real headache! In Python we have the same functionality but we have a function called copy.deepcopy() that copies JUST the value and does not create a link. Is there a similar function in Codesys please?

    Thanks S

     
  • sgronchi - 2022-10-06

    Are you really sure? What types are a and b in your example?

     
  • hermsen

    hermsen - 2022-10-06

    The IEC61131-3 language is named ST, STL is the Siemens variant (a dialect) of IEC ST.

    You assumption is incorrect.
    Regard the following;

    A:=B;

    A is assigned the value of B;

    So if B was 3, after execution of this statement, A will contain 3. The memory space of variable A contains the value '3' and not a pointer to the address of B.

    You can check this out online by executing the following code;

    IF xRunOnce THEN
    A := B;
    xRunOnce := FALSE;
    END_IF;

    Just assign B with 3, and run the code.
    Make xRunOnce High, now A gets assigned B. After this assign B with anything else then 3.

    A keeps the value 3, while B will change value. Only after making xRunOnce TRUE will A be assigned with value B.
    Entirely as expected.

    Regards

     
  • fajean - 2022-10-06

    As mentioned above, the assignment operator performs a copy. If the variable being copied is a pointer, then it is the pointer (a memory address) that is copied, so while it still remains a copy technically speaking, it does not create a new instance of whatever is referenced by the pointer. Using ":=" with references applies copy semantics. Using "REF=" with a reference on the left has the same semantics as using ":=" on a pointer, as no data is copied, just the reference being made to point to the designated entity.

    This being said, the post's title refers to a "deep copy". Maybe the question is about what happens to elements that are nested inside whatever is being copied (such as variables inside a function block when the function block is assigned).

    In that case, the response is : the same thing. If, for instance, you perform an assignment between to variables the type of which is a function block, both copies are identical after the assignment. If they contain nested variables, function blocks or structures, the copied nested elements are identical. If they contain references or pointers, the reference or pointer is copied, so that both copies will point/reference the same memory space after the assignment (referenced entities are NOT duplicated). I suspect that, under the hood, assignments are implemented with just a straight memory copy. You can say this is a "deep copy" in a sense.

    If you would like an operator that creates mirror copies of entities targeted by pointers or references, I do not know (nor do I think) that this exists in CODESYS. I have not had cases where I would like that to exist, and I use "{attribute 'no_assign'}" in all my function blocks so that attempts to copy instances (e.g. using ":=" instead of "REF=") causes errors. Obviously, it is likely other people have different use cases or different approaches where having this feature would be neat. But it is likely that, for that to be generally useful, some pointers/referenced would have to be so duplicated, while others would retain the usual semantics : there would need to be some kind of marker to tell which.

     

Log in to post a comment.