Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c++ > #701

Re: Is a union a kind of class?

From evansl <cppljevans@googlemail.com>
Newsgroups comp.std.c++
Subject Re: Is a union a kind of class?
Date 2013-12-29 23:00 -0600
Organization unknown
Message-ID <f9355f69-7099-433e-9337-e5558381e238@googlegroups.com> (permalink)
References <52204496.5050408@bawi.org> <24874a99-32a8-475d-88c2-36631dfb45ba@googlegroups.com> <c8rtu.34611$O25.31034@fx21.iad> <52B84DE2.2070604@verizon.net>

Show all headers | View raw


On Friday, December 27, 2013 1:11:16 PM UTC-6, James Kuyper wrote:
> On 12/22/2013 08:27 PM, news wrote:
>>
>> On 12/19/13 13:24, johnolithicsoftware@googlemail.com wrote:
>> [snip]
>>
>>> The size of a union is equal to the size of it's largest data member
>>> if I'm not mistaken.
>>
>> What about:
>>
>>    union dbl_charray
>>    {
>>        double dbl;
>>        char [sizeof(double)+1] charray;
>>    };
>>
>> IIUC, in order to satisfy the alignment requirements of the double,
>> the size would have to be 2*sizeof(double), not just sizeof(double)+1.
>
> Not necessarily. The minimum possible size will be sizeof(double) +
> alignof(double), which could be smaller than 2*sizeof(double)

Ah!  I see my error.  The reason this works is because,
to satisfy the "space_requirement":

     sizeof(double)+alignof(double) >= sizeof(dbl_charray::dbl)
  && sizeof(double)+alignof(double) >= sizeof(dbl_charray::charray)
  //thus making room for the largest member of the union.

and, because sizeof(double)%alignof(double)==0,
to satisfy the "alignment_requirement":

  (sizeof(double)+alignof(double))%alignof(double)==0
  //thus assuring that the 2nd element of a dbl_charray[2]
  //is properly aligned for the dbl_charray::dbl.
  //No need to worry about dbl_charray::charray because
  //it has the minimum alignment, 1.

> - in principle, it could be as small as sizeof(double) + 1.

I don't understand, unless alignof(double) == 1; otherwise, I can't
see how the "alignemnt_requirement" is satisfied.

Could you explain a little more about this "in principle" statement?

[snip]

TiA.

-regards,
Larry


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]

Back to comp.std.c++ | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Is a union a kind of class? Seungbeom Kim <musiphil@bawi.org> - 2013-08-31 01:12 -0600
  Re: Is a union a kind of class? Daniel Krügler <daniel.kruegler@googlemail.com> - 2013-08-31 23:52 -0700
  Re: Is a union a kind of class? James Kuyper <jameskuyper@verizon.net> - 2013-08-31 23:52 -0700
  Re: Is a union a kind of class? johnolithicsoftware@googlemail.com - 2013-12-19 11:24 -0800
    Re: Is a union a kind of class? "news" <news@fx21.iad.highwinds-media.com> - 2013-12-22 19:27 -0600
      Re: Is a union a kind of class? James Kuyper <jameskuyper@verizon.net> - 2013-12-27 13:11 -0600
        Re: Is a union a kind of class? evansl <cppljevans@googlemail.com> - 2013-12-29 23:00 -0600
          Re: Is a union a kind of class? James Kuyper <jameskuyper@verizon.net> - 2013-12-31 17:45 -0600

csiph-web