Lazarus

Free Pascal => General => Topic started by: trn76 on April 29, 2014, 10:53:27 pm

Title: Typed pointers = operator behavior?
Post by: trn76 on April 29, 2014, 10:53:27 pm
How exactly does a = operator compare when it comes to types pointer...
Code: [Select]
PTestXY = ^TTestXY;
TTestXY = record
   x: integer;
   y: integer;
end;

procedure ablbaslalbasbl;
var: a, b: PTestXY;
begin
   ....some other code, imagine we have assigned the same pointer to both a and b!

   if a = b then begin
      // do some stuff if pointer "a" is "b"
   end;

end;
I would believe the operator = means: "a" is pointing at same address as "b" - but no, it did compare something(?) else.... !? I had to change to:
Code: [Select]
   if Addr(a) = Addr(b) then begin
      // do some stuff if pointer "a" is "b"
   end;

...What am I missing here, I can't see the logic here!??
Title: Re: Typed pointers = operator behavior?
Post by: marcov on April 29, 2014, 10:54:55 pm
It should simply compare the address. But that is hard to test with only partial code. Try to reduce it to a minimal, preferably console program.
Title: Re: Typed pointers = operator behavior?
Post by: trn76 on April 29, 2014, 11:05:16 pm
It should simply compare the address. But that is hard to test with only partial code. Try to reduce it to a minimal, preferably console program.
Yeah, that was my belief too... I had to do a sanity check, thanks - I'll debug and try and see if I spot the bug.
Title: Re: Typed pointers = operator behavior?
Post by: Yogi on April 29, 2014, 11:08:26 pm
Pointer-Arithmetik can be difficult
 On an Intel machine a pointer consitst of two values: Segment and Offset.
Different Segments and different Offsets can point to the same location but have different values (Seg:Ofs  in Decimals => 1000:0000 would be the same location as 0999:0001 but the pointer value is different). Therefore You first have to "normalize" the pointers to the same Seg and that is what Adr does in Your case.
Title: Re: Typed pointers = operator behavior?
Post by: howardpc on April 29, 2014, 11:15:26 pm
Depending on the setting of the compiler option
Code: [Select]
{$T}the compiler will (or will not) compare the implied types, as well as generating code to compare the addresses.
Title: Re: Typed pointers = operator behavior?
Post by: Yogi on April 29, 2014, 11:35:34 pm
I dont think that {$T} will help in that case. But im sure this one will work correctly independant from the switch: Just cast the pointer to ptrint
 :-[
Code: [Select]
if ptrint(pointer1)=ptrint(pointer2) then . . .
Title: Re: Typed pointers = operator behavior?
Post by: Cyrax on April 30, 2014, 06:35:56 am
Pointer-Arithmetik can be difficult
 On an Intel machine a pointer consitst of two values: Segment and Offset.
Different Segments and different Offsets can point to the same location but have different values (Seg:Ofs  in Decimals => 1000:0000 would be the same location as 0999:0001 but the pointer value is different). Therefore You first have to "normalize" the pointers to the same Seg and that is what Adr does in Your case.

This only applies if you are developing a 16-bit program. In 32-bit and 64-bit environments there is only offsets.
Title: Re: Typed pointers = operator behavior?
Post by: circular on April 30, 2014, 07:41:58 am
trn76, what do you see in the assembler window? (debug with a breakpoint on the line and open assembler window)
TinyPortal © 2005-2018