Recent

Author Topic: Typed pointers = operator behavior?  (Read 4919 times)

trn76

  • New Member
  • *
  • Posts: 40
Typed pointers = operator behavior?
« 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!??

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Typed pointers = operator behavior?
« Reply #1 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.

trn76

  • New Member
  • *
  • Posts: 40
Re: Typed pointers = operator behavior?
« Reply #2 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.

Yogi

  • New Member
  • *
  • Posts: 42
Re: Typed pointers = operator behavior?
« Reply #3 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.
Lazarus 1.3 SVN 44197 * FPC 2.7.1 SVN 26822
Linux: 3.2.0-4-686-pae * Debian wheezy
Windows: W98

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Typed pointers = operator behavior?
« Reply #4 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.

Yogi

  • New Member
  • *
  • Posts: 42
Re: Typed pointers = operator behavior?
« Reply #5 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 . . .
« Last Edit: April 29, 2014, 11:37:21 pm by Yogi »
Lazarus 1.3 SVN 44197 * FPC 2.7.1 SVN 26822
Linux: 3.2.0-4-686-pae * Debian wheezy
Windows: W98

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Typed pointers = operator behavior?
« Reply #6 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.

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: Typed pointers = operator behavior?
« Reply #7 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)
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018