This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Adding UNION/MAP -- Feedback and tips pls!


>> "However, if you overlay one variable with another smaller variable,
>> that portion of the initial variable is retained that is not overlaid.
>> Depending on the application, the retained portion of an overlaid variable
>> may or may not contain meaningful data and can be utilized at a later
>> point in the program."
>
>
> Ouch.
>
> This seems to be at odds with C's unions, where it is not allowed to do
> type punning.

As of gcc 4.4.6, the description above seems to match the C behavior:

#include <stdio.h>
#include <stdint.h>

int main() {

  union {
    uint32_t test32int;
    uint16_t test16int;
  } testunion;

  testunion.test32int = 0xFFFFFFFF;
  printf("Before assignment: %0x\n", testunion.test32int);
  testunion.test16int = 0x0000;
  printf("After assignment: %0x\n", testunion.test32int);

}


Produces the following:

Before assignment: ffffffff
After assignment: ffff0000


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]