Lazarus
Free Pascal => General => Topic started by: trn76 on April 29, 2014, 10:53:27 pm
-
How exactly does a = operator compare when it comes to types pointer...
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:
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!??
-
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.
-
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.
-
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.
-
Depending on the setting of the compiler option
{$T}
the compiler will (or will not) compare the implied types, as well as generating code to compare the addresses.
-
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
:-[
if ptrint(pointer1)=ptrint(pointer2) then . . .
-
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.
-
trn76, what do you see in the assembler window? (debug with a breakpoint on the line and open assembler window)