tags:

views:

236

answers:

2

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

+1  A: 

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

georg
+2  A: 

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; 
T.E.D.
Yes, it does work.
Simon Wright
Yes, it does. I didn't find anything on explicit dereferencing of access variables, so I assumed that there is no such thing in Ada. I know better now, thanks!
georg
You can also do a `'access` to get a pointer, which is a smidge more useful. You really shouldn't be doing either in Ada except in some very rare circumstances though. I suggest you go to appendix K (Language-defined Attributes) of your friendly neighborhood Ada LRM and read clean through it. There's lots of good stuff in there.
T.E.D.