1

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?

Guiorgy
  • 1,405
  • 9
  • 26

1 Answers1

1

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.

Guiorgy
  • 1,405
  • 9
  • 26
Filippo Boido
  • 1,136
  • 7
  • 11
  • I am NOT talking about the size of the memory the pointer points to, but rather the pointer itself. From the documentation we read: 'on 64-bit platforms, when the pointers are 64-bit pointers'. Also I get a warning when I try to add a DINT variable to the pointer that it's an implicit conversion to WORD in one project, and LWORD on another. so, how much memory space does the POINTER take? 4 bytes or 8 bytes? Please correct your answer to reflect the question. – Guiorgy Sep 04 '20 at 17:46
  • What do you need the size of the pointer for?The pointer stores the address to your variable.It makes sense that the address of a 64bit system is 8 bytes and the size of an 32bit system address is 4 bytes.For me is way more relevant and practical the fact that the size of primitive datatypes does not change when switching between 32bit and 64bit runtime. – Filippo Boido Sep 04 '20 at 19:54
  • Pointers aren't new to me, coming from c++.The memory they point to is not relevant to my question. If you must know, one reason I want to know the size of pointers is for debugging. I wanted a way to store, record and log the address of the memory pointers pointed to, thus I needed a way to read their literal value and compare them. So I was using a pointer to DWORD to defer the value of pointers as DWORD, which worked on some simulations, but not on other, and confused me. As it stands now, I can't rate your answer, as it is completely off point. Please, change it so I may evaluate/accept it – Guiorgy Sep 05 '20 at 11:33