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: a07f3367d7,4cad17e8664256c9 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!news1.google.com!news.glorb.com!news2.glorb.com!transit3.readnews.com!news-out.readnews.com!news-xxxfer.readnews.com!panix!bloom-beacon.mit.edu!newsswitch.lcs.mit.edu!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Allocation questions Date: Thu, 28 May 2009 19:13:58 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <4a1e5d9e$0$2868$ba620e4c@news.skynet.be> <96a89bf4-4968-45fb-aa5b-4a47f2e06045@f19g2000yqo.googlegroups.com> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: pcls6.std.com 1243552439 17119 192.74.137.71 (28 May 2009 23:13:59 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Thu, 28 May 2009 23:13:59 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:1b047bjSXWKZP7kEpa5BvwwrTaE= Xref: g2news2.google.com comp.lang.ada:6091 Date: 2009-05-28T19:13:58-04:00 List-Id: Ludovic Brenta writes: > xavier grave: >> > Or you could use a discriminant: >> >> > � �type Samples (No_Of_Samples : Natural := 0) is >> > � � � record >> > � � � � �S : array (1 .. No_Of_Samples) of Sample; >> > � � � end record; >> >> My two cents about variant discriminant : IMHO one should avoid to >> declare No_Of_Samples as Natural and with a default value, because as >> far as I remember some compilers will book for S (1 .. Natural'Last) of >> Sample which can be a lot... >> >> May be some compiler guru can confirm or infirm this ? > > I confirm this with GNAT even though I'm no guru. Well spotted, > Xavier. A defaulted discriminant means you are allowed to declare unconstrained objects of the type. Yes, GNAT and some other compilers will allocate the max size for such unconstrained objects. In this case, that's some gigabytes, which you never want. GNAT warns on the above, I think. If you create constrained objects of type Samples, it will use the right size. But if all your objects are constrained, there's no point in having the default. You want one of: type Samples (No_Of_Samples : Natural) is ... -- You can only have constrained objects, and they will tend to -- have reasonable size. Or: subtype My_Range is Natural range 0..1000; -- or some tolerable max type Samples (No_Of_Samples : My_Range := 0) is ... -- Now you can have unconstrained objects, and waste a modest amount -- of memory. When in doubt, don't have a default. It's also less efficient for parameter passing, because the compiler has to pass extra information "is this thing constrained". Without the default, the compiler knows all objects are constrained. Except if the type is limited: then you can use defaults on discrims freely, no problem. - Bob