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=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Variant record memory storage question Date: Thu, 2 Aug 2018 00:30:42 +0200 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <6cfb0a23-91d0-4ebe-9291-426280e12913@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 1 Aug 2018 22:30:43 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="d6f04e705fb27a4e7123e471c37292d5"; logging-data="25955"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19n2EQPHorYw5MKbTSmBHaEXbtNvYSa4DA=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 Cancel-Lock: sha1:UVY7dW8/LTUNhI/PrwwowaYjtqw= In-Reply-To: <6cfb0a23-91d0-4ebe-9291-426280e12913@googlegroups.com> Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:54015 Date: 2018-08-02T00:30:42+02:00 List-Id: 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