Re: ++ of C in ada



"nicolas.b" <nicolas.blanpain@xxxxxxxxxxxxxxxxxx> writes:
> How can i implement the operator ++ in ada :
>
> type T_Ptr is access all T_Item;
> How can i implement : procedure Increment (Ptr : in out T_Ptr);
> thanks

Don't do this.

In C, arrays and pointers are interchangeable, and pointer++ is used
to traverse an array.

In Ada, arrays and pointers are different. You cannot increment a
pointer, but you can increment an array index:

type T_Item_Array is array (Positive range <>) of T_Item;

procedure Proc (A : in out T_Item_Array) is
begin
for J in A'Range loop
A (J) := ...
end loop;
end Proc;

The above demonstrates how Ada handles arrays of varying sizes,
without the need for pointers (the compiler uses pointers behind the
scenes, and also ensures you don't go past the end of the array,
forget to deallocate your array, pass the wrong array size to Proc, or
other such mistakes).

If you are not doing arrays, the answer to your question is probably
still "don't do this". If you explain what you are trying to do, we
will explain the proper Ada way (or ways) of doing it. In general,
pointer arithmetic is useful only when interfacing directly with your
hardware.

HTH

--
Ludovic Brenta.
.



Relevant Pages

  • Re: Ada exception block does NOT work?
    ... >> Ada was certainly not the first language with exceptions. ... There are also exceptions in the hardware world, ... For example, pointers should be called ... An index into an array can be used as a pointer to a particular ...
    (comp.lang.ada)
  • Re: Differance between Array and Pointers
    ... Well, except that arrays tend to be much larger than pointers, and the ... An array is a contiguous region of memory containing N elements of M ... indexing vs. a loop). ...
    (comp.arch.embedded)
  • [RFCv2][PATCH] flexible array implementation
    ... I call it a flexible array. ... storage for pointers to the second level. ... all locking must be provided by the caller. ... make sure to pass in &ptr instead of ptr. ...
    (Linux-Kernel)
  • [RFC][PATCH] flexible array implementation v4
    ... I call it a flexible array. ... so never does an order>0 allocation. ... storage for pointers to the second level. ... all locking must be provided by the caller. ...
    (Linux-Kernel)
  • Re: [RFC][PATCH] flexible array implementation v4
    ... I call it a flexible array. ... so never does an order>0 allocation. ... storage for pointers to the second level. ... all locking must be provided by the caller. ...
    (Linux-Kernel)