home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,725 of 33,346   
   Bart van Ingen Schenau to All   
   Re: Fun with unions   
   13 Dec 12 06:09:34   
   
   From: bart@ingen.ddns.info.invalid   
      
   On Wed, 12 Dec 2012 19:42:48 -0800, fmatthew5876 wrote:   
      
   > But how about this:   
   >   
   > union B {   
   > float x;   
   > struct {   
   > float r;   
   > };   
   > };   
   >   
   > Do the members x and the anonymous struct containing r have a "common   
   > initial sequence" namely of a single float?   
      
   No, they don't have a "common initial sequence", because x is not a   
   member of a struct (which in turn would be a member of the union). So   
   technically the results would not be guaranteed if you write to r and   
   then read the value of x.   
   But, a compiler would have to go out of its way to give it other behaviour   
   than that which is mandated for common initial sequences, because the   
   assertion "B b; assert(&b.x == &b.r);" must still hold. There may not be   
   any padding at the start of a POD-struct and the members of a union must   
   be allocated as if they are all the initial member.   
      
   Bart v Ingen Schenau   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca