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.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM 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 Path: g2news1.google.com!postnews.google.com!40g2000yqe.googlegroups.com!not-for-mail From: belteshazzar Newsgroups: comp.lang.ada Subject: Re: Unconstrained Arrays Date: Tue, 17 Mar 2009 16:00:53 -0700 (PDT) Organization: http://groups.google.com Message-ID: <9f518ae2-3e67-447a-86ef-787c1437505a@40g2000yqe.googlegroups.com> References: <1a8008fb-c840-45bc-824c-d10eec9fe569@d36g2000prf.googlegroups.com> <0caa9cf8-0620-4544-9b2c-2c9f24142b7f@v23g2000pro.googlegroups.com> <73194169-cc24-428b-8f9e-c0740982bc79@i20g2000prf.googlegroups.com> NNTP-Posting-Host: 203.36.107.146 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1237330854 4420 127.0.0.1 (17 Mar 2009 23:00:54 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 17 Mar 2009 23:00:54 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: 40g2000yqe.googlegroups.com; posting-host=203.36.107.146; posting-account=SuaatgoAAADZMrKGiLdPOjCBBS4KZzVT User-Agent: G2/1.0 X-HTTP-Via: 1.1 SRV021 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30),gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:4165 Date: 2009-03-17T16:00:53-07:00 List-Id: On Mar 18, 1:33=A0am, Adam Beneschan wrote: > On Mar 16, 7:58 pm, belteshazzar wrote: > > > > > > > On Mar 17, 11:49 am, "Jeffrey R. Carter" > > > wrote: > > > belteshazzar wrote: > > > > I have a program that uses a lot of different sized arrays and pass= ed > > > > 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 poo= l 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 tha= t i > > > > have to use array initializers so that they remain unconstrained > > > > rather than becoming an anonomous contrained type that can no longe= r > > > > be passed to my math functions. > > > > I'm not clear what you're talking about. You can pass a constrained s= ubtype to a > > > subprogram that takes a parameter of an unconstrained array type. > > > > For example, String is an unconstrained array type. If we have > > > > function F (S : in String) return Natural; > > > > V : String (1 .. 10); > > > C : Natural; > > > > then it's perfectly legal to call F with V as its actual parameter: > > > > C :=3D F (S); > > > > > Also, and the main point of my post, I've found that I can place th= e > > > > unconstrained array inside a record with a distriminant and this se= ems > > > > 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. > > > > Here is your problem. There should be no reason to pass explicit poin= ters to > > > these functions. Your best solution is to rewrite or change your libr= ary. > > > > -- > > > Jeff Carter > > > "Drown in a vat of whiskey. Death, where is thy sting?" > > > Never Give a Sucker an Even Break > > > 106 > > > As we have very large array's we're using something like: > > > type Unconstrained_Array is array ( Integer range <> ) of Integer; > > type Unconstrained_Array_Pointer is access all Unconstrained_Array; > > > procedure F (S : in Unconstrained_Array_Pointer); > > > V : Unconstrained_Array :=3D (1 .. 10_000 =3D> 0); > > V_Ptr : Unconstrained_Array_Ptr :=3D > > Unconstrained_Array'unchecked_Access > > > F (V_Ptr); > > > Note the use of the array intialiaser, if this isn't used then the > > pointer is no longer compatible. > > As Jeffrey C. and Jeffrey C. have pointed out, you shouldn't use > pointers in your library routines. =A0Declare your library routines to > have parameters of type Unconstrained_Array and don't worry about > access types at all. =A0Ada makes things real simple if you let it. > > If for some reason you really need to use a pointer (like maybe your > library is frozen and you can't change it without trying to sneak a > library change requisition form past a vicious CM man-wolf), I think > that in Ada 2005 you can improve performance with something like this: > > V : aliased Unconstrained_Array :=3D (1 .. 10_000 =3D> <>); > V_Ptr : Unconstrained_Array_Ptr :=3D V'Unchecked_Access; > > This uses the "default" initializer for the element type. =A0Since the > default initializer for Integer is to leave it as undefined garbage, > the compiler shouldn't generate any code to set up the array. =A0No > guarantees, though; your compiler may do something anyway. > > Also, if you were able to use 'Unchecked_Access without making V > aliased, your compiler is broken. > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -- Adam- = Hide quoted text - > > - Show quoted text - Unfortuantely this is a 2005 feature and we're using 95 ... but thanks for the tip.