Search code examples
structuresizeoftwincat

Twincat 3 - SizeOf returning wrong structure size


I have a structure, and am trying to get the size of this structure. SizeOf returns 16, but I am expecting 14 as answer.

2+2+4+2+2+2=14

By using pointers I noticed that there are 2 empty bytes at the end of the structure.

If I replace the UDINT with UINT then the size is correct. If I put the UDINT at the end of the structure, then the two empty bytes are placed after iCrateCnt.

This leads me to believe that the sizeOf is working properly, but for some unknown reason there are two additional bytes placed somewhere in my structure that I am not using.

Why is this happening and how can it be solved?

enter image description here


Solution

  • The pack_mode attribute can be used to eliminate unused bytes in a structure.

    https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/2529746059.html&id=3686945105176987925