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 33,258 of 33,346   
   danielgutson@googlemail.com to All   
   new bitfield value to ensure structure p   
   20 Nov 13 18:55:04   
   
   { Please limit your text to fit within 80 columns, preferably around 70,   
     so that readers don't have to scroll horizontally to read each line.   
     This article has been reformatted manually by the moderator. -mod }   
      
   Now that we have zero-length bit fields for alignment control,   
   I was wondering if we could address the reverse problem, i.e.   
   avoiding the compiler to align.   
      
   For example, consider:   
      
   struct FogFrameHeader   
   {   
       uint8_t         : 4;   
       uint8_t valid   : 1;   
       uint8_t         : 1;   
       uint8_t id      : 2;   
   };   
      
   struct FogFrame   
   {   
       FogFrameHeader header;    // This will unlikely take 1 byte   
       uint8_t dummy;   
       int32_t data :22;   
       int32_t      :10;   
   };   
      
   In the example above, there is no guarantee that sizeof(FogFrame)   
   will be 1 + 1 + 4.   
      
   My prosal is to enable specifying a bit field length of -1 to tell the   
   compiler that in such position it should not insert spaces, or in other   
   words, that the previous and next members should be adjacent in memory.   
   With this syntax, the FogFrame would end as:   
      
   struct FogFrame   
   {   
       FogFrameHeader header;   
       uint8_t: -1;                // No spaces here   
       uint8_t dummy;   
       uint8_t: -1;                // Ditto   
       int32_t data :22;   
       int32_t      :10;   
   };   
      
   Nowadays, this behavior can be obtained in most compilers by specifying   
   the "packed" attribute, but I'm looking for a stadard way to force it.   
      
   Suggestions?   
      
      Daniel.   
      
      
   --   
         [ 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