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,55a8252137b5ef97 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!proxad.net!newsfeed.stueberl.de!newsfeed01.sul.t-online.de!newsmm00.sul.t-online.de!t-online.de!news.t-online.com!not-for-mail From: Martin Krischik Newsgroups: comp.lang.ada Subject: Re: Efficiently setting up large quantities of constant data Date: Tue, 14 Dec 2004 09:43:59 +0100 Organization: None Message-ID: <1351015.VGMk0IRZiT@linux1.krischik.com> References: Reply-To: martin@krischik.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: news.t-online.com 1103015189 04 10701 VX-9XXD6nqKaY9L 041214 09:06:29 X-Complaints-To: usenet-abuse@t-online.de X-ID: b0x0dTZrZeexQa-2OVzERYT2C9bfPvEAJwIt6pjrpUkiCBGfKR+E6B User-Agent: KNode/0.8.0 Xref: g2news1.google.com comp.lang.ada:6934 Date: 2004-12-14T09:43:59+01:00 List-Id: Michael Mounteney wrote: > I want to set up about 100 variables which somehow reference > variable-length arrays of other things. The other things, for a given > variable, are fixed. Simplifying: > > type component is > record > X : character; > end record; Since you come from C: Ada "subtype" is C "typedef" - type is a different concept. You only need: type component is new Character; if you need Character type without implicit convertions. > type component_list is array (positive range <>) of component; > type component_ref is access all component_list; Even repeating myself: don't use "access all" until you need it. "access all" might need additional runtime checks to be performed. The GNAT compiler will actualy isue an error message telling you when "access all" is needed. > type structure is > record > part : component_ref; > end record; You could join that: type component_list is array (positive range <>) of component; type structure (size : positive) is record part : component_list (1 .. size); end record; Save you the use of heap memory. > So I will have lots of structures scattered about, which will be > completely constant. I would like to enforce this constancy, but Ada > (as far as I know) does not (unlike C++; that should get some hackles > rising) allow `constant' to be splashed about in type definitions. > This is on GNAT, which does not support pragma read_only. Somehow true. But almost any named type can be made constant. See http://en.wikibooks.org/wiki/Programming:Ada:Subtypes#named_subtype for what a named type is. > Is there any way to ensure that these data are not modified, other > than by forbidding direct access to structure.part etc? GNAT has an interesting optimization: type String_Contant is access constant String; My_Constant : contant String_Contant := new String'("Some Test"); will not call malloc (GNAT uses C malloc and free internaly) but uses static memory instead. Since this optimization is suggested by the RM other compilers are likely to behave the same. Martin -- mailto://krischik@users.sourceforge.net http://www.ada.krischik.com