gpt4 book ai didi

ada - 如何打印 ada 访问变量指向的地址?

转载 作者:行者123 更新时间:2023-12-05 00:41:41 42 4
gpt4 key购买 nike

我想打印访问变量(指针)的地址以进行调试。

type Node is private;
type Node_Ptr is access Node;

procedure foo(n: in out Node_Ptr) is
package Address_Node is new System.Address_To_Access_Conversions(Node);
use Address_Node;
begin
Put_Line("node at address " & System.Address_Image(To_Address(n)));
end foo;

Address_Image 返回地址的字符串表示形式。
System.Address_To_Access_Conversions 是一个在地址和访问类型之间转换的通用包(参见 ARM 13.7.2 ),定义如下:
generic
type Object(<>) is limited private;
package System.Address_To_Access_Conversions is
-- [...]
type Object_Pointer is access all Object;
-- [...]
function To_Address(Value : Object_Pointer) return Address;
-- [...]
end System.Address_To_Access_Conversions;

gnat 给了我上面定义的过程 foo 的以下错误:
expected type "System.Address_To_Access_Conversions.Object_Pointer" from instance at line...found type "Node_Ptr" defined at ...

Object_Pointer ist definied as access all Object. From my understanding the type Object is Node, therefore Object_Ptr is access all Node. What is gnat complaining about?
I guess my understanding of Ada generics is flawed and I am not using System.Address_To_Access_Conversions correctly.

EDIT:I compiled my code with "gnatmake -gnatG" to see the generic instantiation:

package address_node is
subtype btree__clear__address_node__object__2 is btree__node;
type btree__clear__address_node__object_pointer__2 is access
all btree__clear__address_node__object__2;
function to_address (value :
btree__clear__address_node__object_pointer__2) return
system__address;
end address_node;

btree__node 是上面定义的 Node 类型的错误名称,所以我真的认为 to_address() 的参数类型是正确的,但 gnat 提示(见上文)。

最佳答案

目前我面前没有编译器,但这行不通?

procedure foo(n: in out Node_Ptr) is 
begin
Put_Line("node at address " & System.Address_Image(n.all'address)); --'
end foo;

关于ada - 如何打印 ada 访问变量指向的地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2783055/

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