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=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,6433f675cae9b5bc X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-03-27 11:50:08 PST Path: supernews.google.com!sn-xit-03!supernews.com!news-xfer.nuri.net!newsfeed.dacom.co.kr!news.maxwell.syr.edu!fr.clara.net!heighliner.fr.clara.net!freenix!enst!enst.fr!not-for-mail From: "Beard, Frank" Newsgroups: comp.lang.ada Subject: RE: Allocate an array of a record with a discriminant? Date: Tue, 27 Mar 2001 14:44:48 -0500 Organization: ENST, France Sender: comp.lang.ada-admin@ada.eu.org Message-ID: Reply-To: comp.lang.ada@ada.eu.org NNTP-Posting-Host: marvin.enst.fr Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: avanie.enst.fr 985722382 98774 137.194.161.2 (27 Mar 2001 19:46:22 GMT) X-Complaints-To: usenet@enst.fr NNTP-Posting-Date: Tue, 27 Mar 2001 19:46:22 +0000 (UTC) To: "'comp.lang.ada@ada.eu.org'" Return-Path: X-Mailer: Internet Mail Service (5.5.2448.0) Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org X-Mailman-Version: 2.0.3 Precedence: bulk List-Help: List-Post: List-Subscribe: , List-Id: comp.lang.ada mail<->news gateway List-Unsubscribe: , List-Archive: Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org Xref: supernews.google.com comp.lang.ada:6135 Date: 2001-03-27T14:44:48-05:00 There a couple ways I can think of off the top of my head. 1) Use a declare block; procedure XYZ is ... begin ... --+ Prompt for Max_Length and Num_Words ... declare subtype Constrained_Word is Word(Max_Length); type Word_Array_Type is array (1..Num_Words) of Constrained_Word; Word_Array : Word_Array_Type; begin ... --+ Input the Words and do your processing. ... end; ... end XYZ; The problem with this method is you have to stay within the declare block, which is fine if it's your main program and pass it down to subprograms. 2) Use Access types. procedure XYZ is ... type Word_Access_Type is access Word; type Word_Array_Type is array (Natural range <>) of Word_Access_Type; type Word_Array_Access_Type is access Word_Array_Type; ... Word_Access : Word_Access_Type; Word_Array_Access : Word_Array_Access_Type; ... begin ... --+ Prompt for Max_Length and Num_Words ... Word_Array_Access := new Word_Array_Type(Num_Words); for I in 1 .. Num_Words loop ... Word_Array_Accss(I) := new Word(Max_Length); ... end loop; ... --+ Input the Words and do your processing. ... end XYZ; Frank PS. It's a good idea to default you values, such as type Word (Max_Length : Integer) is record Length : Integer := 0; S : String (1 .. Max_Length) := (others => ' '); end record; And if you default Max_Lenth, as in: type Word (Max_Length : Integer := 100) is record Length : Integer := 0; S : String (1 .. Max_Length) := (others => ' '); end record; You could use the record directly in the Word_Array_Type I declared above, except that using Integer as the base range will cause the compiler to reserve 2147483647 characters as the maximum possible variant of each element of the array. Which will probably result in Storage_Error. If you want to go that route, you need to define your own type with a smaller range. -----Original Message----- From: Joe [mailto:sorry@no.email] Sent: Tuesday, March 27, 2001 10:42 AM To: comp.lang.ada@ada.eu.org Subject: Allocate an array of a record with a discriminant? Dear all, I'd appreciate it greatly if any of you could answer the following question. Basically, I want to create an array of English words. The following information will be given before the words are typed in, _but_ not before runtime: - the maximum length of any one word (the Max_Length variable will contain it) - the number of words to be given (Num_Words) A word would be described by something like the following type: type Word (Max_Length : Integer) is record Length : Integer; S : String (1 .. Max_Length); end record; As I understand it, using discriminants with arrays is not possible such as: type Words (Max_Word_Length : Integer) is array (Natural range <>) of Word (Max_Word_Length); How is it possible to allocate space for an array of type "Word" where all are of the same "Word.Length" which is "Max_Length" at runtime? Keep in mind that Max_Length and Num_Words are given at runtime, not before. Thanks in advance, Joe _______________________________________________ comp.lang.ada mailing list comp.lang.ada@ada.eu.org http://ada.eu.org/mailman/listinfo/comp.lang.ada