comp.lang.ada
 help / color / mirror / Atom feed
From: eachus@spectre.mitre.org (Robert I. Eachus)
Subject: Re: Help: Dynamic-size arrays using record discriminant.
Date: 1998/10/13
Date: 1998-10-13T00:00:00+00:00	[thread overview]
Message-ID: <EACHUS.98Oct13160432@spectre.mitre.org> (raw)
In-Reply-To: 361E3D0C.41AAC13B@classnet.co.il

In article <361E3D0C.41AAC13B@classnet.co.il> Alon Matas <amatas@classnet.co.il> writes:

   -- I have this problem:
   -- There is a record with a discriminant (and a default value to the
   -- discriminant). The record contains a constrained array, and the range
   -- of the array depends on the discriminant. For example:

   -- The question is how do I change the array size (i.e, the discriminant
   -- value) WITHOUT loosing its current content...

   Easy, make the discriminant an element of the record.

   If you insist on being clever, do the following.  First insure that
your compiler actually allocates the maximum size necessary for the
largest possible value of the object.  Next, declare a type which
overlays exactly the discriminated record type, but where the
discriminant is replaced by a array component.  Now use address
clauses to do the aliasing and do what you want...

   Or you can just realize that some compilers will create the objects
in a way that requires hidden heap allocations and pointers, and from
the fact that your declarations:

        type ARRAY_TYPE  is array (INTEGER range <>) of NATURAL;
        type RECORD_TYPE (SIZE: INTEGER:= 5) is
                record
                        VECTOR: ARRAY_TYPE (1..SIZE);
                end record;

    don't lead to a compiler warning, that you are using such an
implementation.  So not only is it necessary to copy the old value to
the new object, but a clever compiler will (depending on how you write
the code) optimize away the "extra" copy.
--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




  parent reply	other threads:[~1998-10-13  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-10-09  0:00 Help: Dynamic-size arrays using record discriminant Alon Matas
1998-10-11  0:00 ` Niklas Holsti
1998-10-13  0:00 ` Robert I. Eachus [this message]
1998-10-16  0:00 ` Samuel Mize
replies disabled

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