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).
A string is a combination of characters. To represent strings, Ada uses the String data type. When declaring the variable, to initialize it, include its value in double-quotes. To declare a string variable, in the parentheses of Put_Line (), include the name of the variable.
with Ada.Text_IO; use Ada.Text_IO; procedure Exercise is begin declare-- Declarations begin-- Initializations end;end Exercise; Displaying the Value of a Variable One way you can use a variable consists of displaying its value to the user. To do this, you can use Put_Line().
To declare a floating-point variable, use the float keyword. To initialize it, assign a number that includes one decimal separator. Here is an example: with Ada.Text_IO; use Ada.Text_IO; procedure Welcome is value : float := 22.41; begin Put_Line("Value = " & float'image(value)); end Welcome;
Integers An integer is a numeric value for a natural number. In Ada, an integral value is represented with the integerdata type. with Ada.Text_IO; use Ada.Text_IO; procedure Exercise is number : integer;begin end Exercise;
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With