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

时间:2010-05-06 17:20:10

标签: ada

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

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被定义为访问所有对象。根据我的理解,Object类型是Node,因此Object_Ptr访问所有Node。什么是gnat抱怨?
我想我对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正在抱怨(见上文)。

2 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:1)

好的,明确的类型转换可以解决问题:

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