gpt4 book ai didi

delphi指针地址

转载 作者:行者123 更新时间:2023-12-03 14:37:18 25 4
gpt4 key购买 nike

在德尔福中:

如何获取指针指向的地址(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 是什么:)

最佳答案

获取某个内容的地址,请使用@运算符或Addr函数。您已经展示了它的正确用法。您获得了iValue的地址并将其存储在iptrValue中。

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

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

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

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

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

iValue 声明保留了另一 block 内存,它可能与前一个声明的内存相邻。由于 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

关于delphi指针地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1171844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com