From: adam@irvine.com (Adam Beneschan)
Subject: Re: Passing out unconstrained arrays??
Date: 1998/07/02
Date: 1998-07-02T00:00:00+00:00 [thread overview]
Message-ID: <6ngn1n$l4i$1@krusty.irvine.com> (raw)
In-Reply-To: 6nghkl$ch3$1@nnrp1.dejanews.com
In article <6nghkl$ch3$1@nnrp1.dejanews.com> dennison@telepath.com writes:
>
> Max_Returned_Items : constant := 5_000;
> -- Bump this up if you get a Too_Many_Items exception
>
> type Item_List is array (Natural range <>) of Integer
>
> subtype Returned_Item_List is Item_List (1..Max_Returned_Items);
Is there any reason not to just make the upper bound Integer'Last?
You're not actually allocating anything of this subtype, and you're
not going to access any array components beyond Num_Items anyway.
> type Item_List_Ptr is access Returned_Item_List;
>
> type Integer_Ptr is access Integer;
>
> procedure C_Get_Data (Num_Data_Items : in System.Address;
> Data_Items : in System.Address);
> pragma Import (C, "get_data", C_Get_Data);
> procedure C_Dealloc (Location : in Item_List_Ptr);
> pragma Import (C, "dealloc", C_Dealloc);
>
> function Get_Data return Item_List is
> Num_Items : Integer;
> Data_Returned : Item_List_Ptr;
> Data : Returned_Item_List;
> begin
> C_Get_Data (Num_Items'address, Data_Returned'address);
>
> -- Do error handling
> if Num_Items.all = 0 then
> -- return a null array
> return (2..1 => 0);
> elsif Num_Items.all > Max_Returned_Items then
> -- Wow. We need to raise the limit.
> raise Too_Many_Items;
> end if;
>
> Data(1..Num_Items.all) := Data_Returned.all(1..Num_Items.all);
> C_Dealloc(Data_Returned);
> return Data(1..Num_Items.all);
>
> end Get_Data;
>
> T.E.D.
-- Adam
parent reply other threads:[~1998-07-02 0:00 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <6nghkl$ch3$1@nnrp1.dejanews.com>]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox