comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Variant record memory storage question
Date: Thu, 2 Aug 2018 00:30:42 +0200
Date: 2018-08-02T00:30:42+02:00	[thread overview]
Message-ID: <pjtcaj$pb3$1@dont-email.me> (raw)
In-Reply-To: <6cfb0a23-91d0-4ebe-9291-426280e12913@googlegroups.com>

On 08/01/2018 07:38 PM, NiGHTS wrote:
> 
> 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 written, a variable

V : Variant_Record (Option => Is_Small);

should use a small amount of memory (typically 2 bytes, or 4 if it's aligning on 
4-byte boundaries). This would be a byte for V.Option and another byte for 
V.Var_1 (and a couple of padding bytes if it needs 4 bytes).

Since V.Option is fixed and cannot be changed, one might think it possible to 
optimize the discriminant away. But since one can also do V.Option'Address, I 
don't think that's often done in practice.

-- 
Jeff Carter
"I didn't squawk about the steak, dear. I
merely said I didn't see that old horse
that used to be tethered outside here."
Never Give a Sucker an Even Break
103

      parent reply	other threads:[~2018-08-01 22:30 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
2018-08-01 22:30 ` Jeffrey R. Carter [this message]
replies disabled

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