Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/23/05 14:50
Read: times


 
#93762 - Padding in unions
Responding to: ???'s previous message
"Whether any "padding" bytes are added between elements by the compiler."

Union elements are overlaid in memory so there won't be any padding between them. A union may, however, be padded out to an address boundary at the high address end should the alignment of a particular element require it. The alignment of the lowest address will be chosen to suit all elements.

If any type used as a union element itself contains padding, for instance a structure, then that padding will have to be taken into account if that structure element is accessed by reference to any other element of the union.

As Michael pointed out it is in fact illegal to store data in a union through one element then retrieve it through another. In reality, things work as expected on most(?) implementations.

A possibly less non-portable method of accessing one type as another, particularly where structure elements are involved, is to scrap the union altogether and use a pointer to the object of interest cast to a pointer to unsigned char. This avoids the structure padding issue and may even be within the bounds of what is allowed by the 'C' standard.

List of 27 messages in thread
TopicAuthorDate
Unions in C            01/01/70 00:00      
   You miss the point completely...            01/01/70 00:00      
   Easy with Union            01/01/70 00:00      
      You can see from the Raghu example...            01/01/70 00:00      
         Platform-dependence            01/01/70 00:00      
            Padding in unions            01/01/70 00:00      
               portability            01/01/70 00:00      
      array=pointer...?            01/01/70 00:00      
         array != pointer            01/01/70 00:00      
         Quirk of C            01/01/70 00:00      
            Read the FAQ            01/01/70 00:00      
               Read the Comment            01/01/70 00:00      
                  Read everything            01/01/70 00:00      
                     Looks the same to me            01/01/70 00:00      
                        This One            01/01/70 00:00      
                        That's the problem            01/01/70 00:00      
                           Good example            01/01/70 00:00      
                              No fun            01/01/70 00:00      
                                 Well...            01/01/70 00:00      
                                 Of course it does!            01/01/70 00:00      
                                    Hmm            01/01/70 00:00      
                        Actually, even less.            01/01/70 00:00      
                           const pointer            01/01/70 00:00      
   O.K you win            01/01/70 00:00      
      Please conclude            01/01/70 00:00      
         Not Exactly            01/01/70 00:00      
         End of wrong stick?            01/01/70 00:00      

Back to Subject List