comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: Aliased
Date: Mon, 20 Jun 2005 16:41:56 GMT
Date: 2005-06-20T16:41:56+00:00	[thread overview]
Message-ID: <olCte.6551$hK3.1438@newsread3.news.pas.earthlink.net> (raw)
In-Reply-To: <1119260078.916073.75200@g47g2000cwa.googlegroups.com>

nblanpain@hotmail.com wrote:
> But in this case, i can't implement the procedure Initialize :
> "This.Ptr_Array := new T_Array (1.. Dynamic_Size);" is illegal
> (Ptr_array is constrained).

If T_Array is constrained, or Ptr_Array designates a constrained subtype 
of T_Array, then this would be illegal.

It would help if you quoted some context in your replies.

It would also help if we knew what you were trying to accomplish, not 
just how you were trying to do it. This looks as if you want a 
variable-size array, but you're asking about access types and aliased 
components. Variable arrays in Ada are usually implemented using an 
unconstrained variable, a variant record with a default discriminant:

Max_Size : constant := 100; -- or whatever
-- It's a good idea to specify a max to avoid Storage_Error,
-- since most compilers will allocate enough storage for the
-- maximum possible size.

subtype Index is Positive range 1 .. Max_Size;

type Fixed is array (Index range <>) of ...;

type Variable_Array (Size : Index := Max_Size) is record
    Value : Fixed (1 .. Size);
end record;

R : Variable_Array;

R := Variable_Array'(Size  => 32,
                      Value => Fixed'(1 .. 32 => ...) );

This provides a bounded array, which can vary between 1 and Max_Size 
elements. An alternative is an unbounded array, usually implemented 
using access and controlled types:

type Fixed is array (Positive range <>) of ...;

type Unbounded_Array is private;

function To_Unbounded (Item : Fixed) return Unbounded_Array;
function To_Fixed (Item : Unbounded_Array) return Fixed;

This would be implemented as

type Fixed_Ptr is access Fixed;

type Unbounded_Array is new Ada.Finalization.Controlled with record
    Ptr : Fixed_Ptr;
end record;

procedure Adjust (Object : in out Unbounded_Array);
procedure Finalize (Object : in out Unbounded_Array);

Of course you hide the use of access types from your client to avoid 
dangling references. Adjust handles deallocation on assignment, and 
Finalize on finalization.

If you really need an unbounded array abstraction, though, you should 
look at the Vector container that will be part of the Ada 0X revision (X 
will probably equal 6). An Ada-95 version is available somewhere; check 
adapower.com or adaworld.com.

-- 
Jeff Carter
"Blessed is just about anyone with a vested interest in the status quo."
Monty Python's Life of Brian
73



  reply	other threads:[~2005-06-20 16:41 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-06-15 16:06 Aliased nblanpain
2005-06-15 16:35 ` Aliased Martin Dowie
2005-06-15 18:56 ` Aliased nblanpain
2005-06-15 21:47   ` Aliased Martin Dowie
2005-06-16  3:15 ` Aliased Steve
2005-06-16 14:03   ` Aliased nblanpain
2005-06-16 19:56     ` Aliased Simon Wright
2005-06-17  7:01       ` Aliased nblanpain
2005-06-17  9:38         ` Aliased Larry Kilgallen
2005-06-19 19:44           ` Aliased nblanpain
2005-06-19 20:33             ` Aliased Jeffrey Carter
2005-06-20  9:34               ` Aliased nblanpain
2005-06-20 16:41                 ` Jeffrey Carter [this message]
2005-06-17  2:15     ` Aliased Jeffrey Carter
2005-06-17  7:02       ` Aliased nblanpain
2005-06-17  2:35     ` Aliased Steve
2005-06-17  7:52       ` Aliased Dmitry A. Kazakov
2005-06-17  4:38     ` Aliased Christoph Grein
2005-06-29 21:10     ` Aliased Robert A Duff
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox