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!feeder.eternal-september.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Variant record memory storage question Date: Wed, 1 Aug 2018 22:54:14 +0300 Organization: Tidorum Ltd 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 X-Trace: individual.net V48iwAAdRpafCTdcTENDaQfdmqljuPvgglUvTagtgqLoJ4avkE Cancel-Lock: sha1:cZ7pFtWAyi7ydXPlaqiB0+Uc8M4= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 In-Reply-To: <6cfb0a23-91d0-4ebe-9291-426280e12913@googlegroups.com> Xref: reader02.eternal-september.org comp.lang.ada:54013 Date: 2018-08-01T22:54:14+03:00 List-Id: 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 := ; 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 . @ .