comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Variant record memory storage question
Date: Wed, 1 Aug 2018 22:54:14 +0300
Date: 2018-08-01T22:54:14+03:00	[thread overview]
Message-ID: <fsehf6FhfpiU1@mid.individual.net> (raw)
In-Reply-To: <6cfb0a23-91d0-4ebe-9291-426280e12913@googlegroups.com>

On 18-08-01 20:38 , NiGHTS wrote:
> Say I had a variant record like this:
>
> type Which_One is (Is_Small, Is_Big);
>
> type Variant_Record (Option : Which_One)
> is record
>    case Option is
>    when Is_Small =>
>       Var_1 : Byte -- 1 byte
>    when Is_Big =>
>       Var_2 : Some_Large_Record -- 1000 bytes
>    end case;
> end record;
>
> Will the record Variant_Record( Is_Small ) be stored as 1 byte? Or
> does it act almost like a C union allocating the memory for
> Variant_Record( Is_Big ) even though it will never morph at
> run-time?

As your type declaration does not give an initial/default value for the 
discriminant (Option) you cannot declare an uninitialized variable of 
this type, but must either constrain it by giving a value to Option:

    V : Variant_Record (Option => Is_Small);

or by giving an initial value to the whole variable:

    V : Variant_Record := <some expression>;

In either case, V.Option is then defined and fixed, and a sensible 
compiler will allocate only as much space as is needed for the variant 
in question.

Note that the same holds for arrays of Variant_Record objects or for 
heap-allocated Variant_Record objects: the value of Option is fixed when 
the object or component is created and cannot change later, but the 
compiler can be expected to allocate only as much space is is needed for 
this value of Option.

Perhaps you intended to provide an initial/default value for Option, as in

    type Variant_Record (Option : Which_One := Is_Small)
    ...

In this case, you can declare variables without an explicit value for 
Option or an explicit whole initial value, as in

    V : Variant_Record;

and you can also change the variant in V by an assignment (of an 
aggregate) to all of V, as in:

   V := (Option => Is_Big, Var_2 => ...);

The amount of memory the compiler allocates for such a variable V is 
compiler-dependent. GNAT (gcc) allocates the maximum amount that might 
be needed, that is, for the Is_Big size in this case.

> I am trying to determine the most memory-efficient way to store a
> complex record and it seems Variant_Record can help me do this, but I
> am unsure if it will work the way I think it will.

It's hard to give more advice without knowing more about your problem. 
Do you have one object of this type, or many? Are you allowed to use 
heap allocation? If you need many objects, are there some limits on how 
many of them are of the Is_Small variant, and how many are Is_Big?

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .

  parent reply	other threads:[~2018-08-01 19:54 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-01 17:38 Variant record memory storage question NiGHTS
2018-08-01 18:35 ` Shark8
2018-08-01 18:51   ` NiGHTS
2018-08-01 19:00     ` G.B.
2018-08-01 18:58 ` G.B.
2018-08-01 19:54 ` Niklas Holsti [this message]
2018-08-01 22:30 ` Jeffrey R. Carter
replies disabled

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