Search code examples
codesys

POINTER bit size


The CODESYS documentation says

The result of the difference between two pointers is of type DWORD, even on 64-bit platforms, when the pointers are 64-bit pointers.

From this, I guessed that pointers in codesys are 32-bit on x86 platforms, and 64-bit on x64 platforms. Is this true?

I tried running CODESYS_Control_Win_V3 and CODESYS_Control_Win_V3 x64 in simulation mode (CODESYS 3.5 SP16) and in both cases the pointers were 64-bit, but I don't have a real x84 PLC (only x64) so I can't verify this on a real device. Could somebody with an x86 PLC test this and give me their results?

EDIT: Strangely enough, I have 2 separate projects open, and in both I tried ptr := ptr + {some DINT variable};, and on one I get the warning Implicit conversion from signed Type 'DINT' to unsigned Type 'LWORD' while on the other one I get Implicit conversion from signed Type 'DINT' to unsigned Type 'DWORD':

enter image description here enter image description here

EDIT2: I tried this in a test project:

    p: POINTER TO STRING := ADR(str);
    pp: POINTER TO POINTER TO STRING := ADR(p);
    sizep: DINT := SIZEOF(p);     // evaluates to 8
    sizepp: DINT := SIZEOF(pp);   // evaluates to 8

Does that mean they are always 8 bytes?


Solution

  • The size of a pointer is 4 Bytes on a 32bits and 8 Bytes on a 64bits runtime.

    The sentence you found in the documentation just says that the compiler expects a DWORD when you do the difference of 2 pointers. Meaning, you will get that warning when you try to do something like this:

    diTest := pTest - pTest2;
    

    diTest beeing a DINT and pTest and pTest2 beeing two pointers.

    Also meaning you may lose some information if you use a DWORD as a result assignment of the difference of 2 pointers on 64bit systems. In fact you will lose 4 bytes.

    DWORD are 4 bytes long and pointers on 64 bit systems are 8 bytes long.

    In order to store the addresses of your pointers in a way that is cross platform use the PVOID type, which is 4 bytes on 32 bit and 8 bytes on 64 bit systems. PVOID is available in the CAA Types library.

    Alternatively, you can use __XWORD, as PVOID is an alias of __XWORD, which is converted into LWORD on 64-bit platforms and DWORD on 32-bit platforms.platforms.