Re: [fpc-pascal] Pointer question

2023-08-11 Thread Hairy Pixels via fpc-pascal
> On Aug 10, 2023, at 11:37 PM, Michael Van Canneyt via fpc-pascal > wrote: > > This is a very dirty trick to get the offset of a field in a record. > > Note that you're not dereferencing a type but a variable of type TAlignCheck > located > at memory address zero. The address of w (this is

Re: [fpc-pascal] Pointer question

2023-08-11 Thread Nikolay Nikolov via fpc-pascal
On 8/11/23 01:23, Hairy Pixels via fpc-pascal wrote: On Aug 10, 2023, at 2:18 PM, Michael Van Canneyt via fpc-pascal wrote: https://www.freepascal.org/docs-html/current/ref/refse15.html#x42-620003.4 This document doesn't really do a great enumerating all the operators so I'm not sure if t

Re: [fpc-pascal] Pointer question

2023-08-10 Thread Michael Van Canneyt via fpc-pascal
On Thu, 10 Aug 2023, Hairy Pixels via fpc-pascal wrote: On Aug 10, 2023, at 4:23 PM, Hairy Pixels wrote: // 4) subscript (inc and dereference in one step) v := i[1]; #4 was not in the list for example so I wonder what others exist. I found another one in the typinfo.pp unit. What do

Re: [fpc-pascal] Pointer question

2023-08-10 Thread Hairy Pixels via fpc-pascal
> On Aug 10, 2023, at 4:23 PM, Hairy Pixels wrote: > > // 4) subscript (inc and dereference in one step) > v := i[1]; > > > #4 was not in the list for example so I wonder what others exist. I found another one in the typinfo.pp unit. What does, 1) taking the address of a type (@TAlignChec

Re: [fpc-pascal] Pointer question

2023-08-10 Thread Hairy Pixels via fpc-pascal
> On Aug 10, 2023, at 2:18 PM, Michael Van Canneyt via fpc-pascal > wrote: > > https://www.freepascal.org/docs-html/current/ref/refse15.html#x42-620003.4 This document doesn't really do a great enumerating all the operators so I'm not sure if the list is complete. I think the list is: var

Re: [fpc-pascal] Pointer question

2023-08-10 Thread Michael Van Canneyt via fpc-pascal
On Thu, 10 Aug 2023, Hairy Pixels via fpc-pascal wrote: On Aug 10, 2023, at 10:49 AM, Sven Barth via fpc-pascal wrote: Then you simply aren't using them often enough. E.g. the RTTI internal code is full of them and additional typecasts would simply muddle up otherwise relatively clear

Re: [fpc-pascal] Pointer question

2023-08-10 Thread Hairy Pixels via fpc-pascal
> On Aug 10, 2023, at 10:49 AM, Sven Barth via fpc-pascal > wrote: > > Then you simply aren't using them often enough. E.g. the RTTI internal code > is full of them and additional typecasts would simply muddle up otherwise > relatively clear code. > Here's a question, what is the full li

Re: [fpc-pascal] Pointer question

2023-08-10 Thread Sven Barth via fpc-pascal
Hairy Pixels via fpc-pascal schrieb am Do., 10. Aug. 2023, 18:30: > > > > On Aug 10, 2023, at 5:43 AM, Bernd Oppolzer via fpc-pascal < > fpc-pascal@lists.freepascal.org> wrote: > > > > PTRADD (p, i) - p is type ANYPTR, i is integer, result is of type ANYPTR > > PTRDIFF (p1, p2) - two pointers, th

Re: [fpc-pascal] Pointer question

2023-08-10 Thread Hairy Pixels via fpc-pascal
> On Aug 10, 2023, at 5:43 AM, Bernd Oppolzer via fpc-pascal > wrote: > > 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 fun

Re: [fpc-pascal] Pointer question

2023-08-10 Thread Hairy Pixels via fpc-pascal
> On Aug 9, 2023, at 4:37 PM, Tomas Hajny via fpc-pascal > wrote: > > No, the offset is not a memory address but just a number of bytes (which is > also the reason why you cannot add the pointers together, but you can add a > number to a pointer. As an example, you could use this operation

Re: [fpc-pascal] Pointer question

2023-08-10 Thread Bernd Oppolzer via fpc-pascal
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 pr

Re: [fpc-pascal] Pointer question

2023-08-10 Thread 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

Re: [fpc-pascal] Pointer question

2023-08-09 Thread Tomas Hajny via fpc-pascal
On August 10, 2023 at 0:06:57 +0200, Hairy Pixels via fpc-pascal wrote: >> On Aug 9, 2023, at 2:54 PM, Tomas Hajny via fpc-pascal >> wrote: >> >>> >>> 1) what does "i := x - x;" do and what is it's purpose and why doesn't "x + >>> x" work the same? >> >> Pointer subtraction is a reverse ope

Re: [fpc-pascal] Pointer question

2023-08-09 Thread Hairy Pixels via fpc-pascal
> On Aug 9, 2023, at 2:54 PM, Tomas Hajny via fpc-pascal > wrote: > >> >> 1) what does "i := x - x;" do and what is it's purpose and why doesn't "x + >> x" work the same? > > Pointer subtraction is a reverse operation to adding a number to a pointer; > the result of the subtraction is the

Re: [fpc-pascal] Pointer question

2023-08-09 Thread Tomas Hajny via fpc-pascal
On August 9, 2023 at 22:14:17 +0200, Hairy Pixels via fpc-pascal wrote: >Playing around I had a more questions about pointer operations I've never used >myself. > >1) what does "i := x - x;" do and what is it's purpose and why doesn't "x + x" >work the same? Pointer subtraction is a reverse op

Re: [fpc-pascal] Pointer question

2023-08-09 Thread Hairy Pixels via fpc-pascal
Playing around I had a more questions about pointer operations I've never used myself. 1) what does "i := x - x;" do and what is it's purpose and why doesn't "x + x" work the same? 2) I've used pointer equality of course but what does "x > p" do and what is its purpose? === var

Re: [fpc-pascal] Pointer question

2023-08-09 Thread Hairy Pixels via fpc-pascal
> On Aug 8, 2023, at 11:53 PM, Sven Barth via fpc-pascal > wrote: > > By default @ returns a *untyped* pointer that can be assigned to any pointer > type. Thus is compatible with Delphi and can be controlled with the > $TypedAddress directive (there's a slight exception to this when you ass

Re: [fpc-pascal] Pointer question

2023-08-08 Thread Sven Barth via fpc-pascal
Hairy Pixels via fpc-pascal schrieb am Mi., 9. Aug. 2023, 04:59: > I just noticed this by accident and I don't understand. Why does " x := > @r; " compile? I would expect it to fail like the next line where I cast to > the explicit type. > > type > TMyRec = record end; > PMyRec = ^TMyRec; > v

[fpc-pascal] Pointer question

2023-08-08 Thread Hairy Pixels via fpc-pascal
I just noticed this by accident and I don't understand. Why does " x := @r; " compile? I would expect it to fail like the next line where I cast to the explicit type. type TMyRec = record end; PMyRec = ^TMyRec; var x: PByte; r: TMyRec; begin x := @r;// works x := PMyR