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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,5b3aa4bc9027f04e X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!news.glorb.com!news2.glorb.com!wn14feed!worldnet.att.net!bgtnsc04-news.ops.worldnet.att.net.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.ada From: anon@anon.org (anon) Subject: Re: Unconstrained Arrays Reply-To: no to spamers (No@email.given.org) References: <1a8008fb-c840-45bc-824c-d10eec9fe569@d36g2000prf.googlegroups.com> X-Newsreader: IBM NewsReader/2 2.0 Message-ID: Date: Tue, 17 Mar 2009 20:14:29 GMT NNTP-Posting-Host: 12.64.186.237 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc04-news.ops.worldnet.att.net 1237320869 12.64.186.237 (Tue, 17 Mar 2009 20:14:29 GMT) NNTP-Posting-Date: Tue, 17 Mar 2009 20:14:29 GMT Organization: AT&T Worldnet Xref: g2news2.google.com comp.lang.ada:5132 Date: 2009-03-17T20:14:29+00:00 List-Id: Most people just use pointer to a string. Then they must write a set of routines to handle the pointers But Ada 95 and Ada 2005 contains package called "Ada.Strings.Unbounded" which was designed to handle this problem. Instead of using type "String" you will use "Unbounded_String". -- For a simplier design: -- -- From "Ada Problem Solving and Program Design", 1992 -- -- Program 10.9 : Specification Page 504 -- Program 10.10: Gives the Body on Page 508 (not posted) -- -- Modified for Ada 95. Some comments have been removed -- easy to adapt for generic package. -- with Ada.Text_IO ; package VString is -- Specification for ADT to handle string variable 1-80 characyer MaxLength : constant Integer := 80 ; subtype Index is Integer range 0..MaxLength ; type VString is Private ; -- exceptions StringOverflow : exception ; EmptyString : exception ; InvalidArgument : exception ; -- operators function MakeVString (S : String) return VString ; function MakeVString (C : Character) return VString ; function EmptyVString return VString ; -- selectors function Length (S : VString) return Index ; function Value (S : VString) return String ; function Head (S : VString) return Character ; -- inquiry function IsEmpty (S : VString) return Boolean ; -- concattenation function "&" (S1, S2 : VString) return VString ; function "&" (S1 : VString; C : Character) return VString ; function "&" (C : Character; S1 : VString) return VString ; function "&" (S1 : VString; S : String) return VString ; function "&" (S : String; S1 : VString) return VString ; -- lexical compariason function "<" (S1, S2 : VString) return Boolean ; function "<=" (S1, S2 : VString) return Boolean ; function ">" (S1, S2 : VString) return Boolean ; function ">=" (S1, S2 : VString) return Boolean ; -- search function locate (Sub : VString; within : VString) return Index ; function locate (Sub : String; within : VString) return Index ; function locate (C : Character; within : VString) return Index ; function Tail (S : String) return VString ; function substring (S : VString; start, size : Index) return VString ; -- I/O procedure Get_Line ( Item : out VString ) ; procedure Put ( Item : VString ) ; procedure Get_Line ( File : Ada.Text_IO.File_Type; Item : out VString ) ; procedure Put ( File : Ada.Text_IO.File_Type; Item : VString ) ; private type VString is record CurrentLength : Index := 0 ; StringPart : String ( 1 .. MaxLength ) := ( others => Ascii.Nul ) ; end record ; end VString ; In <1a8008fb-c840-45bc-824c-d10eec9fe569@d36g2000prf.googlegroups.com>, belteshazzar writes: >I have a program that uses a lot of different sized arrays and passed >into math functions. So I'm using an unconstrained array type. The >problem that I have is that I'm not allowed to use "new" this means >that I have to use pools (which I can't because to intantiate a pool I >have to constrain the array type) or allocated the arrays on the >stack. Allocating the arrays on the stack is fine, BUT it means that i >have to use array initializers so that they remain unconstrained >rather than becoming an anonomous contrained type that can no longer >be passed to my math functions. > >We have now noticed that because of the size of our arrays that the >array intialisation is causing significant performance issues. Thus, >we need a new way of handling our arrays. > >The best option seems to be to turn the functions that take arrays >into generic functions and instantiate them on the fly according to >the required size. Does anyone know if there are performance issues >with dynamically creating instantiations of generic functions all over >teh place? > >Also, and the main point of my post, I've found that I can place the >unconstrained array inside a record with a distriminant and this seems >to solve all our problems. We don't have to use array initialisers and >we can get pointers to aliased objects that can be easily passed to >the math functions. > >Has anyone come across these sorts of issues? I'm curious as to the >solution that others have found, and why Ada seems to handle >unconstrained arrays differently to unconstrained arrays inside a >record.