Why does FPC behave this way? Maybe we should create an issue? lazarus / 21-General ---------------------------------------------------------------------- ALLIGATOR · 2026-01-11 05:55 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program app;{$mode objfpc} var  p1: Pointer = nil;  p2: Pointer = nil;  r: NativeInt;begin  r := p2 - p1; // OK  r := p2 + p1; // Errorend. As far as I'm concerned, either option should be OK, or both options should be Error ---------------------------------------------------------------------- Thaddy · 2026-01-11 06:22 Delphi does not accept this at all: [dcc32 Error] Project11.dpr(9): E2015 Operator not applicable to this operand type [dcc32 Error] Project11.dpr(10): E2015 Operator not applicable to this operand type It looks a bug. Also: pointers are unsigned by nature, but that does not matter here.. ---------------------------------------------------------------------- 440bx · 2026-01-11 08:37 Quote from: ALLIGATOR on January 11, 2026, 06:55:22 amAs far as I'm concerned, either option should be OK, or both options should be Error No.  What FPC does in those cases is what should be done. The difference between two pointers is the distance between the addresses something which is often needed. The addition of two pointers is meaningless, therefore the compiler is right in rejecting the addition.  That said, if for some reason it would be needed to carry out such an operation, simply typecast the second pointer to ptruint and the compiler will then interpret it as a distance instead of a pointer thereby allowing the operation (I've never needed to add two pointers... ever!... the result is meaningless and quite likely totally useless... cannot think of an example, not even a contrived one, where it might be useful) Conclusion: the compiler is right.  Subtraction is logically sound.  Addition is meaningless, therefore rejected. ---------------------------------------------------------------------- Thaddy · 2026-01-11 08:51 That leaves the small matter that Delphi does not accept both, although I think your reasoning is OK for mode objfpc. ---------------------------------------------------------------------- 440bx · 2026-01-11 09:01 Quote from: Thaddy on January 11, 2026, 09:51:50 amThat leaves the small matter that Delphi does not accept both, although I think your reasoning is OK for mode objfpc. Delphi isn't always right (that said, it is much more often right than wrong.) It's been a while since I used Delphi (Delphi 2 in my case) but, I remember taking the difference between two pointers in Delphi 2, what I don't remember is if I had to typecast the pointers for the operation to be allowed. ---------------------------------------------------------------------- ALLIGATOR · 2026-01-11 09:21 Quote from: 440bx...Yes, that makes sense ---------------------------------------------------------------------- MarkMLl · 2026-01-11 10:25 Quote from: Thaddy on January 11, 2026, 09:51:50 amThat leaves the small matter that Delphi does not accept both, although I think your reasoning is OK for mode objfpc. That could be explained if Delphi does not attempt to use "The Delphi Language" to write low-level memory management code. I remember a 1970s book called something like "Writing Linkers and Loaders", the title of which would suggest that it was oriented towards mainframes. This made the argument that integers and pointers were a fairly close parallel to scalars and vectors in that there were permissible and illegal combinations: you can index relative to a pointer by adding or subtracting an integer, you can meaningfully subtract (but not add) and compare pointers for equality, you can't multiply pointers... and so on. So while subtraction of pointers might be one of those operations best inaccessible to application-level programmers, it's definitely needed. MarkMLl ---------------------------------------------------------------------- Thaddy · 2026-01-11 10:51 I agree that the distance between raw pointers is useful indeed. I guess that part is intentional. ---------------------------------------------------------------------- simone · 2026-01-11 11:30 If I remember correctly, even in C, subtraction between pointers is allowed, but addition is not. ---------------------------------------------------------------------- Thaddy · 2026-01-11 12:09 Just tested D7: same errors, both + and - not accepted by Delphi. My previous post was test with D12.1 ---------------------------------------------------------------------- 440bx · 2026-01-11 13:50 Quote from: simone on January 11, 2026, 12:30:56 pmIf I remember correctly, even in C, subtraction between pointers is allowed, but addition is not. I remember successfully subtracting pointers in C.  I can't say anything about adding them because I've never needed that. ---------------------------------------------------------------------- Thaddy · 2026-01-11 14:30 C (any flavor) will not accept the addition without casts. C will accept the difference without cast, like FPC does. ---------------------------------------------------------------------- tetrastes · 2026-01-12 09:05 Quote from: MarkMLl on January 11, 2026, 11:25:05 amI remember a 1970s book called something like "Writing Linkers and Loaders", the title of which would suggest that it was oriented towards mainframes. This made the argument that integers and pointers were a fairly close parallel to scalars and vectors in that there were permissible and illegal combinations: you can index relative to a pointer by adding or subtracting an integer, you can meaningfully subtract (but not add) and compare pointers for equality, you can't multiply pointers... and so on. Fairly close parallel? For sure you can add and multiply vectors. ---------------------------------------------------------------------- H₂SO₄ · 2026-01-12 10:59 Quote from: tetrastes on January 12, 2026, 10:05:36 amFairly close parallel? For sure you can add and multiply vectors. Component-wise, scalar product or cross product?  In any case, has anyone tried this with typed pointers in Delphi? It's important to note that e.g.  PByte(A) - PByte(B) <> PInteger(A) - PInteger(B),  which is required to uphold the indexing identity  Ptr[Index] = (Ptr+ Index)^  (i.e. pointee size must be taken into account). Since  void*  is an incomplete type and thus has no well-defined size, pointer arithmetic on  void*  is forbidden in C and C++.  Maybe Delphi is just following suit? ---------------------------------------------------------------------- 440bx · 2026-01-12 11:39 Actually, there is no good reason to disallow the subtraction of two plain/untyped pointers in Pascal. A plain/untyped pointer is an address, the type it points to is unknown since it is untyped but, there is no doubt whatsoever that it is an address.   Subtracting two untyped addresses yields the number of memory units between those two addresses and, in today's computers, a memory unit is a byte.  Therefore, the subtraction of two untyped pointers should yield the number of bytes between the two addresses. The really important thing when subtracting pointers is that they MUST be of the same type, otherwise the subtraction is senseless. For instance, subtracting a pointer to a boolean from a pointer to a qword, makes no sense because the unit of distance between those pointers/addresses cannot be established. In Pascal, an untyped pointer is the address of the minimum addressable memory unit, i.e, a byte, subtraction of such pointers should be allowed just as they are allowed on ^byte (since they are implicitly pointers to a byte.) Note that in C, a void* is _not_ semantically identical to a Pascal untyped pointer because in C, there is the concept of a void item/object.  That does not exist in Pascal, i.e, there is no ^; (pointer to semicolon, which would be the Pascal "pointer to empty", overloading the semicolon to mean not only empty statement but "empty anything" (which is C's void))  IOW, by definition, in C a void* doesn't point to a byte instead it points into nothingness and it makes no practical sense to subtract nothingness from nothingness.  If C allowed the subtraction of two void* then the only possible result would be zero reqardless of their value because they both point to the same thing: nothing! ---------------------------------------------------------------------- Thaddy · 2026-01-12 11:54 In C taking the difference of two pointers IS allowed. ---------------------------------------------------------------------- marcov · 2026-01-12 12:08 Such subtractions have been long used to get fields offsets (@pa.field-@pa.firstfield). Older delphis required integer casts for that, and a lot of "integer" was used, which caused problems with 64-bit. So newer versions of Delphi and FPC allow more with pointers to avoid integer typecasts. Incrementing pointers with pointers however has no meaning. ---------------------------------------------------------------------- H₂SO₄ · 2026-01-12 12:19 Quote from: 440bx on January 12, 2026, 12:39:38 pm[...] it makes no practical sense to subtract nothingness from nothingness. This got me thinking: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TEmpty = record end;  PEmpty = ^TEmpty; function PtrDiff(A, B: PEmpty): PtrInt;begin  Result := B - A; // No warningsend; Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i mov rax, rsisub rax, rdiret GCC says  error: arithmetic on pointer to an empty aggregate,  while Clang just warns that  subtraction of pointers to type 'struct Empty' of zero size has undefined behavior. How does Delphi handle this? ---------------------------------------------------------------------- BrunoK · 2026-01-12 13:45 Quote from: Khrys on January 12, 2026, 01:19:04 pm This got me thinking: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TEmpty = record end;  PEmpty = ^TEmpty; function PtrDiff(A, B: PEmpty): PtrInt;begin  Result := B - A; // No warningsend; Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i mov rax, rsisub rax, rdiret GCC says  error: arithmetic on pointer to an empty aggregate,  while Clang just warns that  subtraction of pointers to type 'struct Empty' of zero size has undefined behavior. How does Delphi handle this? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;  type  TEmpty = record  end;  PEmpty = ^TEmpty;   function PtrDiff(A, B: PEmpty): PtrInt;  begin    Result := B - A; // No warnings  end;   procedure TestPtrDiff;  var    pe1, pe2: PEmpty;  begin    WriteLn(PtrDiff(pe1, pe2));  end; var  a, b: integer;  vBool: boolean;  e1, e2: TEmpty;  pe1, pe2: PEmpty;begin  vBool := (Pointer(pbyte(A) - pbyte(B)) = Pointer(PInteger(A) - PInteger(B)));  WriteLn(vBool, LineEnding);   WriteLn(PtrDiff(pe1, pe2),LineEnding);   pe1 := @e1;  pe2 := @e2;  WriteLn(PtrDiff(pe1, pe2),LineEnding);  TestPtrDiff;  ReadLn;end. Compiles with messages and warnings : Quoteproject1.lpr(18,29) Warning: Local variable "pe2" does not seem to be initialized project1.lpr(18,24) Warning: Local variable "pe1" does not seem to be initialized project1.lpr(27,21) Warning: Conversion between ordinals and pointers is not portable project1.lpr(27,32) Warning: Conversion between ordinals and pointers is not portable project1.lpr(27,27) Warning: Variable "a" does not seem to be initialized project1.lpr(27,38) Warning: Variable "b" does not seem to be initialized project1.lpr(27,13) Hint: Conversion between ordinals and pointers is not portable project1.lpr(27,52) Warning: Conversion between ordinals and pointers is not portable project1.lpr(27,66) Warning: Conversion between ordinals and pointers is not portable project1.lpr(27,44) Hint: Conversion between ordinals and pointers is not portable project1.lpr(30,27) Warning: Variable "pe2" does not seem to be initialized project1.lpr(30,22) Warning: Variable "pe1" does not seem to be initialized What is there more to ask ? Quote from: marcov on January 12, 2026, 01:08:44 pmSuch subtractions have been long used to get fields offsets (@pa.field-@pa.firstfield). Older delphis required integer casts for that, and a lot of "integer" was used, which caused problems with 64-bit. That's all there is to it, as marcov comments rightly. Using calculated offsets to record that would be made of variant records in unions can use the mechanism to write / append consistent union-ed sub records. If you don't need that mechanism, the don't use it ... ---------------------------------------------------------------------- marcov · 2026-01-12 15:49 Quote from: Khrys on January 12, 2026, 01:19:04 pmGCC says  error: arithmetic on pointer to an empty aggregate,  while Clang just warns that  subtraction of pointers to type 'struct Empty' of zero size has undefined behavior. How does Delphi handle this? It depends on how you define the subtraction. Number of elements or number of bytes (as used for offsets). In FPC it depends on $T. With $T-, Subtraction of typed pointers still return a byte offset in FPC. When $T+ it returns the number of elements. Both are 0 if the record type is empty, no warnings. My older Delphi (Seattle, 10.0) doesn't compile it, complaining about "operator not applicable to this operand type". ---------------------------------------------------------------------- VisualLab · 2026-01-12 16:30 Quote from: 440bx on January 12, 2026, 12:39:38 pmNote that in C, a void* is _not_ semantically identical to a Pascal untyped pointer because in C, there is the concept of a void item/object.  That does not exist in Pascal, i.e, there is no ^; (pointer to semicolon, which would be the Pascal "pointer to empty", overloading the semicolon to mean not only empty statement but "empty anything" (which is C's void))  IOW, by definition, in C a void* doesn't point to a byte instead it points into nothingness and it makes no practical sense to subtract nothingness from nothingness.  If C allowed the subtraction of two void* then the only possible result would be zero reqardless of their value because they both point to the same thing: nothing! Is this really the case? Various online tutorials claim that: void informs the compiler that nothing will appear at a given location, i.e.: if void is specified in parentheses after the function name, it means that the function doesn't actually take any arguments,if void is specified on the left side of the function name (separated by a space), it means that the function doesn't actually return any result, void* is a pointer to something (amorphous, amorphous), but it's not nothingness like the first case; it's a pointer without type information, and that's it. So void (without *) and void* (with *) are two different things. The first is information about the absence of anything, the second is a pointer to something. So "void*" is basically the same as Pointer in Pascal. The mess is due to the idiotic syntax of the C language, invented by Denis R. This is prone to code errors. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; type  TEmpty = record end;  PEmpty = ^TEmpty;  PNothing = Pointer; function PtrDiffEmpty(A, B: PEmpty): PtrInt;begin  Result := B - A; // No warningsend; function PtrDiffNothing(A, B: PNothing): PtrInt;begin  Result := B - A; // No warningsend; var  X, Y: PEmpty;  K, L: PNothing; begin // New(X); // New(Y);  Writeln('Result "Empty": ', PtrDiffEmpty(X, Y));  Writeln('Result "Nothing": ', PtrDiffNothing(K, L));  Readln;end. The compiler reports that: Quoteproject1.lpr(25,48) Warning: Variable "Y" does not seem to be initialized project1.lpr(25,45) Warning: Variable "X" does not seem to be initialized project1.lpr(26,52) Warning: Variable "L" does not seem to be initialized project1.lpr(26,49) Warning: Variable "K" does not seem to be initialized But the code compiles. The results are shown in the attachment. ---------------------------------------------------------------------- Thaddy · 2026-01-12 16:58 Quote from: Khrys on January 12, 2026, 01:19:04 pmHow does Delphi handle this? Delphi 7 and Delphi 12.x do not allow it at all and does not warn but errors. That is unless you apply a cast festival. (Same as for C, except C allows pointer difference, even if with a warning in modern C compilers) ---------------------------------------------------------------------- 440bx · 2026-01-12 17:12 Quote from: VisualLab on January 12, 2026, 05:30:41 pmSo void (without *) and void* (with *) are two different things. The first is information about the absence of anything, the second is a pointer to something. So "void*" is basically the same as Pointer in Pascal. The mess is due to the idiotic syntax of the C language, invented by Denis R. This is prone to code errors. There is a problem in that interpretation. You got the first part right, void is the absence of anything.  Creating a pointer to void doesn't change what void itself is.  void* is a pointer to the "absence of anything" which is a pointer to nothing.  What you stated in the second part is akin to saying that an integer is not the target of a pointer to integer.  The fact that it is a pointer to integer doesn't change the integer in any way.  Same thing with void, void is void whether by itself or the target of a pointer. void is defined in the C standard.  There is no equivalent in Pascal, i.e, void is something that does not exist in Pascal.  In Pascal there is no such thing as "nothing"/"void".  Just in case, for the record and without pun intended, an empty record is _not_ what void is in C. That's the reason C compilers complain about some operations on void*.  A void* in C is _not_ a pointer to a byte.  Whereas in Pascal a "pointer" is implicitly a pointer to a byte, which is why it is sensible to subtract two plain/untyped pointers in Pascal, while in C, it is a dubious operation due to the definition of "void". I don't know what Delphi does but FPC has it right.  You can increment or decrement an untyped pointer and, that's as it should be.  OTH, that said, it would be clearer to cast the pointer to pbyte.  That would make the programmer's intention clear instead of relying on compiler behavior that may not be obvious. ---------------------------------------------------------------------- VisualLab · 2026-01-12 18:31 Quote from: 440bx on January 12, 2026, 06:12:15 pmThere is a problem in that interpretation. Yes. Quote from: 440bx on January 12, 2026, 06:12:15 pmvoid is defined in the C standard.  There is no equivalent in Pascal, i.e, void is something that does not exist in Pascal.  In Pascal there is no such thing as "nothing"/"void".  Just in case, for the record and without pun intended, an empty record is _not_ what void is in C. Yes, and very good, because it was Denis's idiotic idea. Wirth rightly assumed that if something does not exist, there is no need to write it down. Thanks to this approach, the code is understandable and clearer. Quote from: 440bx on January 12, 2026, 06:12:15 pmThat's the reason C compilers complain about some operations on void*.  A void* in C is _not_ a pointer to a byte.  Whereas in Pascal a "pointer" is implicitly a pointer to a byte, which is why it is sensible to subtract two plain/untyped pointers in Pascal, while in C, it is a dubious operation due to the definition of "void". Yes, but void* is not a pointer to nothing (emptiness). So what is a pointer that stores nullptr (in Pascal: nil)? void* stores a real address (when it does not store nullptr), since it can be cast to a typed pointer. It is only unknown what object is located at this address. But it's not void. Otherwise, the cast wouldn't be possible (and it wouldn't make sense). It's simply a stupid way to represent the fact that it's a pointer to anything. Stupid because Denis used a previously defined word but with a slight modification (asterisk). And it caused confusion. People are reading "God knows what" into it. And this is just an idiotic notation. Now, after all these years, it's hard to figure out what Denis really meant when he came up with "void*." Perhaps after many years Denis no longer wanted to comment on the genesis of his idiotic ideas. After all, he was a renowned computer scientist, so it's understandable that he didn't want to lose his "crown" In any case, C has many such bizarre ideas, the origins of which have become mythologized over the years. Both the textbooks and the C language descriptions now resemble the Talmud (Catechism, or other interpretations): a collection of commentaries on commentaries, which in turn were commentaries on even older commentaries, which were on even older ones, and somewhere at the end, after many layers, were the original references to the Torah. Something that had been put together over years. Such a "multi-layered manipulation", where the original meaning has been completely blurred (or maybe even lost). The only thing that is widely repeated nowadays is the myth that: "the creator of C was an exceptionally brilliant computer scientist and created an IT miracle." And if someone tries to analyze this from a technical perspective and, "God forbid," tries to point out flaws in the C language's design, they are considered an IT blasphemer. Because such time-honored dogmas cannot be questioned. Fortunately, this forum is rarely visited by "IT inquisitors" Quote from: 440bx on January 12, 2026, 06:12:15 pmI don't know what Delphi does but FPC has it right.  You can increment or decrement an untyped pointer and, that's as it should be.  OTH, that said, it would be clearer to cast the pointer to pbyte.  That would make the programmer's intention clear instead of relying on compiler behavior that may not be obvious. Yes. ---------------------------------------------------------------------- PascalDragon · 2026-01-12 21:01 Quote from: Thaddy on January 12, 2026, 05:58:13 pmQuote from: Khrys on January 12, 2026, 01:19:04 pmHow does Delphi handle this? Delphi 7 and Delphi 12.x do not allow it at all and does not warn but errors. That is unless you apply a cast festival. (Same as for C, except C allows pointer difference, even if with a warning in modern C compilers) Or enable $PointerMath: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program tptrdiff; {$APPTYPE CONSOLE}{$POINTERMATH ON} type  TEmpty = record  end;  PEmpty = ^TEmpty; var  p1: PEmpty = nil;  p2: PEmpty = nil;  r: NativeInt;begin  r := p2 - p1; // OK  //r := p2 + p1; // Errorend. ---------------------------------------------------------------------- ALLIGATOR · 2026-01-13 03:22 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program app; var  p1: Pointer;  p2: Pointer;  r:  Pointer; function AddPointers(l, r: Pointer): Pointer; inline;var  null: Pointer = nil;begin  result := l - (null - r);end; begin  r := AddPointers(p1, p2);end. I wonder how likely it is that this is a shot in the foot? ---------------------------------------------------------------------- 440bx · 2026-01-13 03:41 Quote from: ALLIGATOR on January 13, 2026, 04:22:47 amI wonder how likely it is that this is a shot in the foot?  What you got there is a roundabout way of adding two pointers. You can achieve the same result by casting the second pointer (r in your example) to ptruint.  if you do that, the compiler will interpret the pointer as a distance from "l" and do the calculation.  Basically, you were successful in adding two pointers but, the result is rather meaningless, therefore utterly useless. Another way of doing it would be to "absolute" a ptruint var on top of "r".  That would allow you to directly add the absoluted identifier to "l". There is more than one way to skin a cat, there is also more than one way for a programmer to shoot (him/her)self in the foot. ---------------------------------------------------------------------- Thaddy · 2026-01-13 09:39 Quote from: PascalDragon on January 12, 2026, 10:01:18 pmOr enable $PointerMath: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TEmpty = record  end;  PEmpty = ^TEmpty; We have that already in trunk and was introduced on my request/patch: https://www.freepascal.org/docs-html/rtl/system/topaquedata.html https://www.freepascal.org/docs-html/rtl/system/popaquedata.html https://www.freepascal.org/docs-html/rtl/system/opaquepointer.html But it is a bit cheating away from untyped pointers... Casting in disguise. (Although this was originally intended to pass C structures around) ---------------------------------------------------------------------- PascalDragon · 2026-01-15 20:26 Quote from: Thaddy on January 13, 2026, 10:39:31 amQuote from: PascalDragon on January 12, 2026, 10:01:18 pmOr enable $PointerMath: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TEmpty = record  end;  PEmpty = ^TEmpty; We have that already in trunk and was introduced on my request/patch: https://www.freepascal.org/docs-html/rtl/system/topaquedata.html https://www.freepascal.org/docs-html/rtl/system/popaquedata.html https://www.freepascal.org/docs-html/rtl/system/opaquepointer.html That's not the point! The point is that a current Delphi does compile a difference of pointers to an empty record if $PointerMath is enabled!