简体   繁体   English

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

[英]How to print the address an ada access variable points to?

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. Address_Image返回地址的字符串表示形式。
System.Address_To_Access_Conversions is a generic package to convert between addresses and access types (see ARM 13.7.2 ), defined as follows: 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 gives me the following errors for procedure foo defined above: 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. Object_Pointer被定义为访问所有Object。 From my understanding the type Object is Node, therefore Object_Ptr is access all Node. 根据我的理解,Object类型是Node,因此Object_Ptr访问所有Node。 What is gnat complaining about? 什么是gnat抱怨?
I guess my understanding of Ada generics is flawed and I am not using System.Address_To_Access_Conversions correctly. 我想我对Ada泛型的理解是有缺陷的,我没有正确使用System.Address_To_Access_Conversions。

EDIT: I compiled my code with "gnatmake -gnatG" to see the generic instantiation: 编辑:我用“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 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). btree__node是上面定义的Node类型的错位名称,所以我认为to_address()的参数类型是正确的,但是gnat正在抱怨(见上文)。

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; 

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... ;-) 需要一段时间习惯阿达... ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM