Recent

Author Topic: Please explain what is Pointer()?  (Read 1918 times)

freemind001

  • Jr. Member
  • **
  • Posts: 51
Please explain what is Pointer()?
« on: February 03, 2025, 10:36:54 am »
Code: Pascal  [Select][+][-]
  1. const
  2.   UI_SET_EVBIT = $40045564;
  3.   EV_KEY = $01;
  4. var
  5.   key: integer;
  6.   kbd: longint = -1;
  7. begin
  8.   kbd := fpOpen('/dev/uinput', O_WRONLY or O_SYNC);
  9.   ioRes += fpIOCtl(kbd, UI_SET_EVBIT, Pointer(EV_KEY));
  10.   for key := 0 to 248 do
  11.     iores += fpIOCtl(kbd, UI_SET_KEYBIT, Pointer(key));
  12. ...
  13. end

This code works as expected, but for the second call of fpIOCtl compiler says
Warning: Conversion between ordinals and pointers is not portable
The first call of fpIOCtl is ok for the compiler.

What is the diffrence between Pointer() of const and of var?
what is the point of casting integer to pointer and what does Pointer(key) actually mean?
Why not @key?
« Last Edit: February 03, 2025, 11:32:11 am by freemind001 »

Khrys

  • Full Member
  • ***
  • Posts: 155
Re: Please explain what is Pointer()?
« Reply #1 on: February 03, 2025, 11:12:32 am »
What is the diffrence between Pointer() of const and of var?

There is no difference; in both instances it's a cast into a generic (untyped) pointer.

what is the point of casting integer to pointer and what does Pointer(key) actually mean?

The declaration of  ioctl  requires that its third argument be a generic pointer. Pointer(key)  simply reinterprets the contents of  key  as a pointer to satisfy the type checker. Ultimately it is up to the specific device driver how exactly this value is used.

Why not @key?

That would take the address of  key,  which is a completely different operation that by itself has nothing to do with casting.

freemind001

  • Jr. Member
  • **
  • Posts: 51
Re: Please explain what is Pointer()?
« Reply #2 on: February 03, 2025, 11:43:02 am »
That would take the address of key,  which is a completely different operation that by itself has nothing to do with casting.

from https://wiki.freepascal.org/Pointer
A pointer is a variable that contains the memory address of a local or global variable, or the memory address of a value or complex object that has been created and stored in the heap part of a program's memory layout.

Sorry, I don't get it. What's the diffrence?

freemind001

  • Jr. Member
  • **
  • Posts: 51
Re: Please explain what is Pointer()?
« Reply #3 on: February 03, 2025, 11:48:11 am »
simply reinterprets the contents of  key  as a pointer to satisfy the type checker.

So, it is not the address of key? (I mean Pointer(key))
What is it then?))

Thaddy

  • Hero Member
  • *****
  • Posts: 16580
  • Kallstadt seems a good place to evict Trump to.
Re: Please explain what is Pointer()?
« Reply #4 on: February 03, 2025, 11:54:05 am »
No, in this case it is a pointer CAST: it mimics the value of key as if it were a pointer.
The real address of key as a raw pointer is @Key or addr(key), but here you don't need that, even more you don't want that because it would make the code wrong.
That is basically the same as Khrys wrote in other words.
« Last Edit: February 03, 2025, 11:58:01 am by Thaddy »
But I am sure they don't want the Trumps back...

440bx

  • Hero Member
  • *****
  • Posts: 5010
Re: Please explain what is Pointer()?
« Reply #5 on: February 03, 2025, 12:32:04 pm »
Maybe an example will make it easier to understand, consider the following code:
Code: Pascal  [Select][+][-]
  1. var
  2.   i : integer = $1234;
  3.   p : pointer;
  4.  
  5. begin
  6.   { attempting to set the value of "p" to be equal to the value of "i" fails because "i" is  }
  7.   { not a pointer, therefore the compiler rejects that statement.                            }
  8.  
  9.   p := i;  { attempt to set "p" to the value $1234 (the value of "i") fails                  }
  10.  
  11.   p := pointer(i); { tells the compiler to treat "i" as if it had been declared to be a      }
  12.                    { pointer instead of an integer (for that single statement)               }
  13.  
  14.   { now the pointer "p" has the value $1234 (the value of "i")                               }
  15.  
  16.   { the "pointer(i);" tells the compiler to forget that "i" is an integer and consider it a  }
  17.   { pointer instead.  Note that this does NOT change the value of "i", it just changes the   }
  18.   { way it is interpreted (in this case, from integer to pointer.)                           }
  19.  
  20.   { pointer(key) simply tells the compiler to forget whatever type "key" was declared to be  }
  21.   { and instead, for that single statement only, act as if it had been declared to be a      }
  22.   { pointer.                                                                                 }
  23. end.
  24.  
Line 9, the assignment "p := i;" will be rejected by the compiler because "i" is an integer and an integer cannot be assigned to a pointer ("p" in this case.)

Line 11, the "pointer(i);" part of the statement tells the compiler that "i" is to be considered a pointer (for that single statement) and, since now "i" is (temporarily) a pointer instead of an integer, it can be assigned to "p".

That temporary re-typing of a variable is called a cast.  It does not change the values stored in the variable, it only changes how they are interpreted (their type, in this case from type integer to type pointer)

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

freemind001

  • Jr. Member
  • **
  • Posts: 51
Re: Please explain what is Pointer()?
« Reply #6 on: February 03, 2025, 12:46:49 pm »
OK, I take it this is a formal operation, in order to satisfy the definition of the function, am I right?
After casting the value is still 0..248, but of type pointer?

How do I get rid of the warning
project1.lpr(25,42) Warning: Conversion between ordinals and pointers is not portable

440bx

  • Hero Member
  • *****
  • Posts: 5010
Re: Please explain what is Pointer()?
« Reply #7 on: February 03, 2025, 12:58:02 pm »
OK, I take it this is a formal operation, in order to satisfy the definition of the function, am I right?
Correct.

After casting the value is still 0..248, but of type pointer?
Correct.

How do I get rid of the warning
project1.lpr(25,42) Warning: Conversion between ordinals and pointers is not portable
TTBOMK, the compiler will emit the warning whenever appropriate (which is likely always),  That said, if you are using Lazarus, you can right click on the line and select one of the "hide" options from the popup menu but, this only works when compiling from Lazarus (which is what most people do.)

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

freemind001

  • Jr. Member
  • **
  • Posts: 51
Re: Please explain what is Pointer()?
« Reply #8 on: February 03, 2025, 01:09:01 pm »
Thank you

440bx

  • Hero Member
  • *****
  • Posts: 5010
Re: Please explain what is Pointer()?
« Reply #9 on: February 03, 2025, 01:26:27 pm »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 16580
  • Kallstadt seems a good place to evict Trump to.
Re: Please explain what is Pointer()?
« Reply #10 on: February 03, 2025, 01:55:50 pm »
You can also compile with the extra option -vq which will show you the warning number and you can subsequently turn that off by {$WARN <xxxx> OFF}
That is what people do that do not use the Lazarus IDE.
I usually specify -vewhnlq to catch al notes, hits, warnings and errors with line numbers and error numbers
But I am sure they don't want the Trumps back...

freemind001

  • Jr. Member
  • **
  • Posts: 51
Re: Please explain what is Pointer()?
« Reply #11 on: February 03, 2025, 03:10:52 pm »
Will there be any problems with this code when compiling on other linux-based operating systems? if so, which ones?
I'm using Ubuntu 24.04 x64

Thaddy

  • Hero Member
  • *****
  • Posts: 16580
  • Kallstadt seems a good place to evict Trump to.
Re: Please explain what is Pointer()?
« Reply #12 on: February 03, 2025, 03:38:36 pm »
No, should not cause any problems.
But I am sure they don't want the Trumps back...

Warfley

  • Hero Member
  • *****
  • Posts: 1865
Re: Please explain what is Pointer()?
« Reply #13 on: February 03, 2025, 08:40:23 pm »
The warning is, as it states, conversion to and from pointers is not necessarily portable. So for example in ARM architectures, pointers need to be aligned, so $xyz0 is a valid pointer while $xyz1 is not.
This is why fpc throws a warning when you cast between those types.

The problem here is a different one, IOCTL can be implemented by many drivers, using different kinds of data (as long as its the size of a pointer), some need pointers, others need integers. This is why the C binding of IOCTL is:
Code: C  [Select][+][-]
  1. int ioctl(int fd, unsigned long op, ...);
Note that it uses varargs to enable this format agnostic implementation. The FPC binding on the other hand is:
Code: Pascal  [Select][+][-]
  1. function FpIOCtl(Handle: cint; Ndx: TIOCtlRequest; Data: Pointer): cint;
Which always takes a pointer, under the assumption that you can just cast your type to pointer.

The Problem here is not the warning, but rather the FPC binding not covering other types, like the C binding does

The reason why the waning is only with a variable, I would assume is because with constants the compiler can check if it is a valid pointer for the target platform, while with a variable, it does not know at compiletime if it is valid or not. But thats just a theory

PascalDragon

  • Hero Member
  • *****
  • Posts: 5870
  • Compiler Developer
Re: Please explain what is Pointer()?
« Reply #14 on: February 04, 2025, 10:02:35 pm »
The reason why the waning is only with a variable, I would assume is because with constants the compiler can check if it is a valid pointer for the target platform, while with a variable, it does not know at compiletime if it is valid or not. But thats just a theory

It has simply to do with the introduction of 64-bit support as users needed to be warned if they cast Integer or LongInt to Pointer or the other way round (which was very common from Delphi). This warning can't really be solved using code, because types like NativeInt are declared as aliases to LongInt or Int64 (or the corresponding 8- or 16-bit types) depending on the platform instead of being compiler internal types which would allow the compiler to drop the warning if NativeInt or NativeUInt is involved.

 

TinyPortal © 2005-2018