2

How to increment an address of access type like a pointer in C? I am new to Ada...

procedure main is
  type myarr is array(1..5)of integer; --creating array of 5 integer.
  myarr_var:aliased myarr:=(2,5,7,9,0); --setting 5 values
  type my_access is access all myarr; --creating access type (pointer)
  var:my_access:=myarr_var'access;  --holding address of 5 integer
begin;
  -- since var holds the address of myarr_var now i want to increment
  -- the address by adding 1 to print next value (5) as we do in c?

  put((var+1).all); --???
  -- i know this is bad but how to increment its base address of 
  -- var(pointer) so that it will point to next element 5?

end main; 
5
  • 3
    why would you want to? Just index the access value as a normal array, dereferencing will then be implicit.
    – egilhh
    Jan 7, 2015 at 18:21
  • @egilhh i was asked in an interview Jan 7, 2015 at 18:32
  • 1
    ok. Unless they are looking for someone to write an OS or possibly a compiler, they will never (yes, bold statement) have pointer arithmetic in their Ada codebase.
    – egilhh
    Jan 7, 2015 at 18:35
  • my_access accesses a 5-element array of integers. So, given C semantics, var++ should point to the next 5-element array of integers (probably 1 past the end of myarr_var). Jan 7, 2015 at 22:42
  • You can't do what you ask for in your comment. Var points to objects of type Myarr, while Myarr_Var (2) is an object of type Integer. Jan 8, 2015 at 9:18

2 Answers 2

6

Instantiate Interfaces.C.Pointers to do C-style pointer arithmetic in Ada.

Best explained by example:

with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C.Pointers;

procedure Access_Pointer_Arithmetic is

   type Myarr_Indices is range 1 .. 5;
   type Myarr is array (Myarr_Indices range <>) of aliased Integer;

   Myarr_Terminator : constant Integer := 0;

   package Myarr_Pointer_Arithmetic is new Interfaces.C.Pointers
     (Myarr_Indices, Integer, Myarr, Myarr_Terminator);

   use Myarr_Pointer_Arithmetic;

   Myarr_Var : aliased Myarr := (2, 5, 7, 9, 0);

   Var : Myarr_Pointer_Arithmetic.Pointer :=
              Myarr_Var(Myarr_Var'First)'access;

begin
   Put_Line(Integer'Image(Var.all));
   Increment(Var);
   Put_Line(Integer'Image(Var.all));
   Increment(Var);
   Put_Line(Integer'Image(Var.all));
   Increment(Var);
   Put_Line(Integer'Image(Var.all));

   Var := Var - 2;
   Put_Line(Integer'Image(Var.all));
end Access_Pointer_Arithmetic;

Running it:

C:\Ada\Sandbox\access_pointer_arithmetic
 2
 5
 7
 9
 5

This package provides single increment/decrement, addition and subtraction of ptrdiff_t, and both terminator-specified and fixed length element array retrieval.

(Mind running off the end of the array... :-)

4
  • thanks but what if i want to increment it by 2 or any no of bytes? Jan 8, 2015 at 17:37
  • 1
    Please review the package to which I linked. There are '+' and '-' operators. Answer updated to demonstrate.
    – Marc C
    Jan 8, 2015 at 18:10
  • some unexpected behavior is noticed Put_Line(Integer'Image(Var.all)); var:=var+5; -- it give 1,var+6 then 2 and if var+7 then 0,var+8 then -1 why please explain? Jan 10, 2015 at 0:58
  • @SantoshGupta You're accessing beyond the end of the array. This is a bad thing. Since you're using C-style pointer incrementing, it's now your responsibility to stay in bounds.
    – Marc C
    Jan 11, 2015 at 18:14
5

UPDATE : I just found out that there's a standard package Interfaces.C.Pointers that directly supports C-style pointer arithmetic, and now I see that Marc C.'s accepted answer covers its usage. You can probably ignore my answer, which discusses how you might do pointer arithmetic in Ada if Interfaces.C.Pointers didn't exist (which, in earlier versions of the language, it doesn't).

If you really want to do C-style pointer arithmetic on Ada access types, you can use the generic package System.Address_To_Access_Conversions to convert an object pointer type to System.Address, and the System.Storage_Elements package to perform C-like arithmetic on System.Address values.

Note that the target object type is a parameter to the System.Address_To_Access_Conversions generic package. The package itself defines the access type. You can't define your own access type and use it (at least not directly).

Keep in mind that C pointer arithmetic is defined in units of the size of the pointed-to object. So given:

int arr[10];
int *ptr = &arr[0];

the pointer value ptr + 3 points to arr[3], which is three int-sized chunks of memory past the location to which ptr points not necessarily three bytes. The "+" and "-" operators in System.Storage_Elements work with offsets in storage elements (which are very likely equivalent to C's "bytes").

So if you have an Ada pointer, um, I mean access value, that refers to an element of an array of Integers, then advancing to the next element of that array requires:

  • Using System.Address_To_Access_Conversions to convert the access type to System.Address;
  • Using the overloaded "+" operator in System.Storage_Elements to add the size of an Integer in bytes (Integer'Max_Size_In_Storage_Elements) to the System.Address value; and
  • Using System.Address_To_Access_Conversions again to convert the System.Address value back to your access type.

An alternative might be to write C code to do whatever pointer arithmetic you need, and use Interfaces.C to call that code from your Ada program.

But it's very likely that you don't need to do pointer arithmetic in Ada. C has pointer arithmetic in the core language; it even defines array indexing in terms of pointer arithmetic. Ada does not. There's rarely a good reason to perform pointer arithmetic in Ada. Just let arrays be arrays, and let the compiler figure out how to generate the best code to access their elements. (That code will likely involve pointer arithmetic on the CPU instruction level.)

7
  • 1
    System.Address_To_Access_Conversions is a better choice than Unchecked_Conversion
    – egilhh
    Jan 7, 2015 at 19:00
  • @egilhh: Right, and System.Storage_Elements provides more or less C-like arithmetic on System.Address values. Thanks, I haven't used Ada a lot lately. Jan 7, 2015 at 19:31
  • 2
    You can use t’Max_Size_In_Storage_Elements to find out how many bytes t occupies. Jan 7, 2015 at 20:03
  • 1
    Plus 1 for mentioning that the compiler will figure out how to generate code that does pointer arithmetic. I suspect the main reason C has pointer arithmetic was to make it easy to take advantage of the PDP-11's auto-increment/decrement addressing modes without making the compiler too complex. But compiler technology has advanced quite a bit in 40+ years.
    – ajb
    Jan 7, 2015 at 20:14
  • @keith Thompson can you please give me small full example i new to ada i tired but couldn't figure out.. i would be very thankful Jan 8, 2015 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.