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

发布于 2024-08-31 09:51:42 字数 1714 浏览 5 评论 0原文

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

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;

对于上面定义的过程 foo,gnat 给出以下错误:

expected type "System.Address_To_Access_Conversions.Object_Pointer" from instance at line...
found type "Node_Ptr" defined at ...

Object_Pointer ist 定义为访问所有对象。根据我的理解,Object 类型是 Node,因此 Object_Ptr 可以访问所有 Node。蚊子在抱怨什么?
我想我对 Ada 泛型的理解是有缺陷的,并且我没有正确使用 System.Address_To_Access_Conversions。

编辑: 我用“gnatmake -gnatG”编译了我的代码以查看通用实例化:

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 正在抱怨(见上文) )。

I want to print the address of an access variable (pointer) for debugging purposes.

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 returns the string representation of an address.
System.Address_To_Access_Conversions is a generic package to convert between addresses and access types (see ARM 13.7.2), defined as follows:

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 gives me the following errors for procedure foo defined above:

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 is the mangled name of the type Node as defined above, so I really think the parameter type for to_address() is correct, yet gnat is complaining (see above).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坐在坟头思考人生 2024-09-07 09:51:42

目前我面前没有编译器,但这不起作用吗?

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

I don't have a compiler in front of me at the moment, but doesn't this work?

procedure foo(n: in out Node_Ptr) is 
begin 
   Put_Line("node at address " & System.Address_Image(n.all'address)); --'
end foo; 
帅的被狗咬 2024-09-07 09:51:42

好的,显式类型转换可以解决问题:

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

需要一些时间来适应 Ada...;-)

Ok, explicit type conversion does the trick:

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

Takes some time getting used to Ada... ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文