How to print the address an ada access variable points to?
- by georg
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).