From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,c22608a39b6f5e7b X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: Re: Dynamic Array Sizing Date: 1999/06/20 Message-ID: #1/1 X-Deja-AN: 491862557 References: <376B1811.666F042@hotmail.com> <376C7D96.47FF7F2@hotmail.com> NNTP-Posting-Date: Sun, 20 Jun 1999 14:15:08 PDT Newsgroups: comp.lang.ada Date: 1999-06-20T00:00:00+00:00 List-Id: On 20 Jun 1999 15:09, Nick Wilson wrote: > The trouble comes about because it's a real time system and my > understanding is that it would be a good idea to do as much of the > initialisation before the system actually starts running. It would be > possible to just open, read from the file however many times as > necessary then close it until the next lookup. I'm just looking for a > nicer way if I can. No, you don't have to open and re-open the file during run-time. File init can be done once, at system start-up. See the code below. > package Lookup_Array is > function Get_Array_Size return Positive; > procedure Initialise_Array; > function Lookup_Value(Index : in Positive) is > end Lookup_Array; > > > -- body > package body Lookup_Array is > > type Array_Store is array (Positive range <>) of Boolean; > Array_Size : Positive; > > function Get_Array_Size return Positive is > Open file > Finds size of array > return Array_Size := the size of array data in the file > end Get_Array_Size; > > declare > The_Array : Array_Store (Array_Size); > > procedure Initialise_Array is > reads values from file into the array > end Initialise_Array; > > function Lookup_Value(Index : in Positive) is > return The_Array(Index); > end Looup_Value; > > end Lookup_Array; > > I know this code isn't right, but hopefully it points out the problem of > where I can declare The_Array so that it is visible to outside calling > functions such as Lookup_Value ? Some comments: o If you insist on declaring a local array, then you can do it during package elaboration: package body Lookup_Array is function Get_Array_Size return Positive is ...; The_Array : Array_Storage (1 .. Get_Array_Size); function Lookup_Value ... is; begin end Lookup_Array; o The client of the package doesn't care whether he's reading from a a local array or from the file directly. Since he's already calling a function to return a value, why not just implement the function to return a value from the file directly: package body Lookup_Array is File : .File_Type; function Lookup_Value (Index : Positive) is Value : ; begin Read (File, Index, Value); return Value; end Lookup_Value; begin Open (File, In_File, "myfile.dat"); end Lookup_Array; No local array is necessary. And all initialization is done once, during elaboration, so there's no run-time penalty.