FWIW, when I added similar functionality to my Stanford Pascal compiler, I chose not to allow arithmetic
of pointers, but instead I added some functions:

PTRADD (p, i) - p is type ANYPTR, i is integer, result is of type ANYPTR
PTRDIFF (p1, p2) - two pointers, the result is integer
ANYPTR is a predefined type, compatible with every (typed pointer)
ADDR (x) is a function (borrowed from PL/1), which returns an ANYPTR ... and it is allowed for all types of variables PTRCAST is the same as PTRADD (p, 0) - and is used to cast pointers between incompatible pointers (not type safe)

Kind regards

Bernd


Am 10.08.2023 um 10:52 schrieb Elmar Haneke via fpc-pascal:
1) what does "i := x - x;" do and what is it's purpose and why doesn't "x + x" 
work the same?

Subtracting pointers may be useful if they point to consecutive memory. The Result is the number of bytes between both addresses.

Adding pointers is useless, you would get a pointer pointing to some address in address space which has no relation to the pointers — presumably accessing it would rise an error.

Therefore, it is a good idea to let the compiler prevent such mistakes.

2) I've used pointer equality of course but what does "x > p" do and what is 
its purpose?

It may be useful if pointers do point into a continuous data object, e.g. a write-pointer inside a buffer.

Elmar

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to