Forum > General
Typed pointers = operator behavior?
trn76:
How exactly does a = operator compare when it comes to types pointer...
--- Code: ---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;
--- End code ---
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: --- if Addr(a) = Addr(b) then begin
// do some stuff if pointer "a" is "b"
end;
--- End code ---
...What am I missing here, I can't see the logic here!??
marcov:
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:
--- Quote from: 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.
--- End quote ---
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:
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.
howardpc:
Depending on the setting of the compiler option
--- Code: ---{$T}
--- End code ---
the compiler will (or will not) compare the implied types, as well as generating code to compare the addresses.
Navigation
[0] Message Index
[#] Next page