How can I efficiently swap pointers in Delphi? I am trying to exchange pointers for integer type. The following example works however I2
is 0 when compiled with 64 bit.
program Project11;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
procedure Swap(const P1, P2);
asm
{$if defined(CPUX86)}
xchg ecx, [eax]
xchg ecx, [edx]
xchg [eax], ecx
{$else}
xchg rcx, [rax]
xchg rcx, [rdx]
xchg [rax], rcx
{$endif}
end;
var
I1, I2: Integer;
begin
I1 := 19;
I2 := 564;
WriteLn('Swapping..');
WriteLn('I1: '+I1.ToString());
WriteLn('I2: '+I2.ToString());
Swap(I1, I2);
WriteLn('I1: '+I1.ToString());
WriteLn('I2: '+I2.ToString());
ReadLn;
end.
TInterlocked.Exchange
. – TmesisLOCK XCHG
– Ibex