【问题标题】:delphi pointer addressdelphi指针地址
【发布时间】:2009-07-23 13:45:04
【问题描述】:

在德尔福中:

如何获取指针指向的地址 (0x2384293)?

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

那么 GetAddress 在现实中是什么 :)

【问题讨论】:

  • GetAddressOf 不存在。它是“在现实中”中所述的伪代码:)

标签: delphi pointers pointer-address


【解决方案1】:

获取某物的地址,请使用@ 运算符或Addr 函数。你已经证明了它的正确使用。你得到了iValue的地址,并将其存储在iptrValue中。

显示地址,您可以使用Format 函数将指针值转换为字符串。使用%p 格式字符串:

Edit1.Text := Format('%p -> %p -> %d', [@iptrValue, iptrValue, iptrValue^]);

这将显示iptrValue 变量的地址,然后是存储在该变量中的地址,然后是存储在该地址的

iptrValue 变量声明在内存中保留了一些字节并将名称与它们相关联。假设第一个字节的地址是$00002468

       iptrValue
       +----------+
$2468: |          |
       +----------+

iValue 声明保留了另一块内存,它可能与前一个声明的内存相邻。由于iptrValue 是四个字节宽,所以iValue 的地址将是$0000246C

       iValue
       +----------+
$246c: |          |
       +----------+

我画的方框现在是空的,因为我们还没有讨论这些变量的值。我们只讨论了变量的地址。现在到可执行代码:你写@iValue并将结果存储在iptrValue中,所以你得到这个:

       iptrValue
       +----------+    +----------+
$2468: |    $246c |--->|          |
       +----------+    +----------+
       iValue
       +----------+
$246c: |          |
       +----------+


Next, you assign 32342 to `iValue`, so your memory looks like this:


       iptrValue
       +----------+    +----------+
$2468: |    $246c |--->|    32342 |
       +----------+    +----------+
       iValue
       +----------+
$246c: |    32342 |
       +----------+

最后,当你从上面显示Format 函数的结果时,你会看到这个值:

00002468 -> 0000246C -> 32342

【讨论】:

  • 嗨@Rob,我认为您的图表具有误导性。它给读者一种感觉,指针指向的内存地址不是 $246c 的盒子。 Imo,最好这样画:i.imgur.com/qXSWa3y.png
【解决方案2】:

只需将其转换为整数:)

IIRC,还有一个字符串格式说明符 (%x? %p?) 会自动将其格式化为 8 个字符的十六进制字符串。

【讨论】:

    【解决方案3】:

    这是我自己的地址函数示例:

    function GetAddressOf( var X ) : String;
    Begin
      Result := IntToHex( Integer( Pointer( @X ) ), 8 );
    end;
    

    使用2个变量相同数据的例子:

    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;
    

    【讨论】:

    • 由于指针和整数之间的大小差异,这并不总是正确的
    【解决方案4】:

    GetAddressOf() 将返回变量的地址。

    GetAddressOf(iptrValue) - the address of the iptrValue
    GetAddressOf(iValue) - the address of iValue
    

    你想要的是指针的值。为此,将指针转换为无符号整数类型(如果我没记错的话,长字)。然后您可以将该整数转换为字符串。

    【讨论】:

      【解决方案5】:

      它实际上是你需要的 ULong:

      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;
      

      在Delphi 2006中没有找到GetAddressOf函数,好像是VB函数?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-30
        • 1970-01-01
        • 1970-01-01
        • 2019-07-15
        相关资源
        最近更新 更多