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,795 of 33,346   
   Gerhard Fiedler to All   
   Re: Fun with unions   
   14 Jan 13 23:29:41   
   
   From: gelists@googlemail.com   
      
   On 2013-01-13 20:23:37, in comp.lang.c++.moderated fmatthew5876 wrote:   
      
   >> AIUI, proving that the addresses are the same doesn't prove that we   
   >> can read and write interchangeably. Couldn't it be that the   
   >> compiler assumes (based on the standard) that this is not possible   
   >> and uses some optimizations that only work if the assumption is   
   >> correct?   
   >>   
   > But would that not violate the fact that if two pointers of the same   
   > type point to the same object then they alias the same address?   
      
   I don't understand what would violate that fact. AIUI, a fact is that   
   the C++ standard doesn't guarantee any specific behavior for reading a   
   union member other than the one that was last written to. And AIUI the   
   compiler is free to perform optimizations that are based on the   
   assumption that you don't read a union member other than the last   
   written to. No?   
      
   >> IMO, what you're trying to prove here is that a struct with   
   >> multiple members of the same type has the same size (and therefore   
   >> alignment) as an array. That's something quite different than what   
   >> you state in this phrase.   
   >>   
   > If the struct and array were the same size and alignment, would it   
   > not then prove that the 2 members of the union can be used   
   > interchangeably?   
      
   Not AIUI -- see the argument about possible optimizations that the   
   compiler may use. I don't have any example at hand, but compiler   
   optimizations are so many that probably nobody knows them all for all   
   common compilers, and IMO it's best not to assume that the compiler   
   will behave in a given way unless that's defined by the standard or   
   the compiler docs.   
      
   >> Couldn't it be that aligning the objects as required by an array is   
   >> suboptimal, and in the array the compiler aligns them suboptimally   
   >> because that is required by the standard, but follows the   
   >> "alignment requirements" for optimal alignment of the given   
   >> platform for the struct case (because it can, according to the   
   >> standard, in the way I understand this)?   
   >   
   > While this idea of suboptimal alignment may be possible in theory   
   > why would any compiler choose to make arrays sub optimal? Arrays are   
   > the bread and butter of anyone trying to write high performance   
   > cache friendly code.   
      
   Because with arrays, the compiler doesn't really have a choice. Assume   
   T to be char[7]. It has a size of 7. An array of these things will   
   have them placed at intervals of 7. If best alignment requires an   
   alignment of, say, 8, this can be used with a struct, but not with an   
   array. Am I missing something?   
      
   (FWIW, it seems that if you are writing high performance code on such   
   a platform, you better avoid arrays of such types. Even if you only   
   need 7 bytes in your T, it may be advantageous to make it 8 bytes   
   wide.)   
      
   Gerhard   
      
      
   --   
         [ 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