Reputation: 1398
In Delphi:
How do I get the address (0x2384293) a pointer points to?
var iValue := Integer;
iptrValue := PInteger;
implementation
procedure TForm1.Button1Click(Sender: TObject);
begin
iptrValue := @iValue;
iValue := 32342;
//Should return the same value:
Edit1.Text := GetAddressOf(iptrValue);
Edit2.Text := GetAddressOf(iValue);
So what is GetAddress in reality :)
Upvotes: 13
Views: 30501
Reputation: 163357
To get the address of something, use the @
operator or the Addr
function. You've already demonstrated correct use of that. You got the address of iValue
and stored it in iptrValue
.
To display an address, you can use the Format
function to convert a pointer value into a string. Use the %p
format string:
Edit1.Text := Format('%p -> %p -> %d', [@iptrValue, iptrValue, iptrValue^]);
That will display the address of the iptrValue
variable, then the address stored in that variable, and then the value stored at that address.
The iptrValue
variable declaration reserves some bytes in memory and associates a name with them. Suppose the address of the first byte is $00002468
:
iptrValue
┌──────────┐
$2468: │ │
└──────────┘
The iValue
declaration reserves another piece of memory, and it will probably be adjacent to the previous declaration's memory. Since iptrValue
is four bytes wide, the address of iValue
would be $0000246C
:
iValue
┌──────────┐
$246c: │ │
└──────────┘
The boxes I've drawn are empty for now because we haven't discussed what values those variables hold. We've only discussed the variables' addresses. Now to the executable code: You write @iValue
and store the result in iptrValue
, so you get this:
iptrValue
┌──────────┐
$2468: │ $246c ├─────╮
└──────────┘ │
iValue │
┌──────────┐ │
$246c: │ │←────╯
└──────────┘
Next, you assign 32342 to iValue
, so your memory looks like this:
iptrValue
┌──────────┐
$2468: │ $246c ├─────╮
└──────────┘ │
iValue │
┌──────────┐ │
$246c: │ 32342 │←────╯
└──────────┘
Finally, when you display the results of the Format
function from above, you would see this value:
00002468 -> 0000246C -> 32342
Upvotes: 36
Reputation: 121
Here is my own example of address function:
function GetAddressOf( var X ) : String;
Begin
Result := IntToHex( Integer( Pointer( @X ) ), 8 );
end;
Example of using 2 variable's same data:
type
TMyProcedure = procedure;
procedure Proc1;
begin
ShowMessage( 'Hello World' );
end;
var
P : PPointer;
Proc2 : TMyProcedure;
begin
P := @@Proc2; //Storing address of pointer to variable
P^ := @Proc1; //Setting address to new data of our stored variable
Proc2; //Will execute code of procedure 'Proc1'
end;
Upvotes: 5
Reputation: 4532
Just cast it to an integer :)
IIRC, there's also a string format specifier (%x? %p?) that will automagically format it as an 8-character hex string.
Upvotes: 5
Reputation: 13203
It actually is a ULong you need:
procedure TForm1.Button1Click(Sender: TObject);
var iValue : Integer;
iAdrValue : ULong;
iptrValue : PInteger;
begin
iValue := 32342;
iAdrValue := ULong(@iValue);
iptrValue := @iValue;
//Should return the same value:
Edit1.Text := IntToStr(iAdrValue);
Edit2.Text := IntToStr(ULong(iptrValue));
Edit3.Text := IntToStr((iptrValue^); // Returns 32342
end;
I did not find the GetAddressOf function in Delphi 2006. It seems to be a VB function?
Upvotes: 2
Reputation: 170539
GetAddressOf() will return the address of the variable.
GetAddressOf(iptrValue) - the address of the iptrValue
GetAddressOf(iValue) - the address of iValue
What you want is the value of the pointer. To do so cast the pointer to an unsigned integer type (Longword if I remember it right). Then you can convert that integer into a string.
Upvotes: 3