comp.lang.ada
 help / color / mirror / Atom feed
From: Martin Krischik <martin@krischik.com>
Subject: Re: Efficiently setting up large quantities of constant data
Date: Tue, 14 Dec 2004 09:43:59 +0100
Date: 2004-12-14T09:43:59+01:00	[thread overview]
Message-ID: <1351015.VGMk0IRZiT@linux1.krischik.com> (raw)
In-Reply-To: fa59671.0412131348.2677f5ec@posting.google.com

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



  parent reply	other threads:[~2004-12-14  8:43 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-13 21:48 Efficiently setting up large quantities of constant data Michael Mounteney
2004-12-13 22:09 ` Stephen Leake
2004-12-13 22:15 ` Luke A. Guest
2004-12-14  0:20 ` Jeffrey Carter
2004-12-14  8:43 ` Martin Krischik [this message]
2004-12-14 12:18 ` Simon Wright
2004-12-15 21:10   ` Brian May
2004-12-16 23:18     ` Nick Roberts
2004-12-16 12:36 ` Dr. Adrian Wrigley
2004-12-16 13:50   ` Marc A. Criley
2004-12-17  2:32     ` John B. Matthews
2004-12-16 14:06   ` rien
2004-12-16 14:24   ` Vinzent 'Gadget' Hoefler
2004-12-16 18:12     ` Dr. Adrian Wrigley
2004-12-16 23:25       ` Nick Roberts
2004-12-17  5:20     ` tmoran
2004-12-17  8:38       ` Vinzent 'Gadget' Hoefler
2004-12-17 13:53     ` Dr. Adrian Wrigley
2004-12-17 14:16       ` Alex R. Mosteo
2004-12-16 17:11   ` Andre
2004-12-16 21:52     ` Larry Kilgallen
2004-12-16 23:23       ` Nick Roberts
2004-12-16 23:47       ` Dr. Adrian Wrigley
2004-12-19 14:34         ` Simon Wright
2004-12-16 18:29 ` Alex R. Mosteo
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox