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

Assigning to a dereferenced pointer

jsaxin
2020-10-13
2020-10-15
  • jsaxin - 2020-10-13

    I have made an array of pointers to places in memory I would like to write to while treating them as a continuous block. But I cannot find the syntax for doing this as all examples of dereferencing has the pointer to the value being copied and not the one being overwritten.

    This triggers a runtime error at the assignment:

    IF xFirstCycle THEN
        apwPointers[0] := ADR(LagerD.Evaporator1.iDefrostHour1);
        xFirstCycle := FALSE;
    END_IF
    
    
    IF Slave.udiWriteAccessCounter <> udiWriteCounter THEN
        apwPointers[Slave.wWriteAddress]^ := awHoldingRegisters[Slave.wWriteAddress];
        udiWriteCounter := Slave.udiWriteAccessCounter;     
    END_IF
    
     

    Last edit: jsaxin 2020-10-13
  • jsaxin - 2020-10-15

    I have found that I never actually initiated the pointer. Please delete this thread.

     
    • hermsen

      hermsen - 2020-10-15

      Some tips after seeing your code snippet:

      To ensure you always work with a valid pointer you should take care of the following:
      Always call the code which references the pointer and not do this only once(!) Doing it continuously ensures correct working during a warm download after a code change, besides the overhead is minimal compared to a crashing application ;-)

      //IF xFirstCycle THEN
          apwPointers[0] := ADR(LagerD.Evaporator1.iDefrostHour1);
      //    xFirstCycle := FALSE;
      //END_IF
      

      Plus always test the validity of the pointer before dereferencing it, even if you are sure that it is initialised properly.
      An escape is to rewrite your code using References, they are "equal" to pointers but are typesafe.

      Hope this helps you

       
      • jsaxin - 2020-10-15

        Thank you. I will look into references.

         
      • jsaxin - 2020-10-15

        Alas, it turns out references cannot be put into arrays.

         
        • hermsen

          hermsen - 2020-10-15

          Ah, I never considered that. But pointers should do fine if you check them <> 0 before trying to dereference them.

           

Log in to post a comment.