delphi pointer address

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 :)

Answers


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 |--->|    32342 |
       +----------+    +----------+
       iValue
       +----------+
$246c: |    32342 |
       +----------+

Finally, when you display the results of the Format function from above, you would see this value:

00002468 -> 0000246C -> 32342

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.


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;

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.


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?


Need Your Help

Ruby: Inherit code that works with class variables

ruby class inheritance metaprogramming

The situation: I have multiple classes that should each hold a variable with a configuration hash; a different hash for each class but the same for all instances of a class.

How to detect if a given number is an integer?

javascript numbers

How can I test a variable to ascertain if it contains a number, and it is an integer?