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.9 required=5.0 tests=BAYES_00 autolearn=ham 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: g2news1.google.com!postnews.google.com!i20g2000prf.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Unconstrained Arrays Date: Tue, 17 Mar 2009 08:33:04 -0700 (PDT) Organization: http://groups.google.com Message-ID: <73194169-cc24-428b-8f9e-c0740982bc79@i20g2000prf.googlegroups.com> References: <1a8008fb-c840-45bc-824c-d10eec9fe569@d36g2000prf.googlegroups.com> <0caa9cf8-0620-4544-9b2c-2c9f24142b7f@v23g2000pro.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1237303985 6378 127.0.0.1 (17 Mar 2009 15:33:05 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 17 Mar 2009 15:33:05 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: i20g2000prf.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:4157 Date: 2009-03-17T08:33:04-07:00 List-Id: 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 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. > > > I'm not clear what you're talking about. You can pass a constrained subtype 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 := F (S); > > > > 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. > > > Here is your problem. There should be no reason to pass explicit pointers to > > these functions. Your best solution is to rewrite or change your library. > > > -- > > 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 := (1 .. 10_000 => 0); > V_Ptr : Unconstrained_Array_Ptr := > 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. Declare your library routines to have parameters of type Unconstrained_Array and don't worry about access types at all. Ada 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 := (1 .. 10_000 => <>); V_Ptr : Unconstrained_Array_Ptr := V'Unchecked_Access; This uses the "default" initializer for the element type. Since the default initializer for Integer is to leave it as undefined garbage, the compiler shouldn't generate any code to set up the array. No 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. -- Adam