After receiving the following statement in an answer to this question:
...you are trying to overlay
value
andbits
, and stuffing data into one alternative of an union and taking it out of the other is undefined.
I became much more curious as to what is allowed (and what is prudent) in regards to type punning in C99. After taking a look around I found a lot of helpful information in the post Is type-punning through a union unspecified in C99....
There was a lot to take away from both the comments and the answers posted there. For the purpose of clarity (and as a sanity-check) I wanted to create an example based on my understanding of the C99 standard. Below is the example code that I created and, while it functioned as I anticipated, I wanted to be certain that my assertions are correct.
The following code contains my assertions in the comments. This is my understanding of type-punning in C99. Are my comments correct? If not, can you please explain why?
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_BYTES sizeof(uint32_t)
typedef union
{
uint32_t fourByteValue;
uint8_t byteValue[NUM_BYTES];
struct
{
unsigned int firstBitSpecified : 1;
unsigned int secondBitSpecified : 1;
unsigned int thirdBitSpecified : 1;
unsigned int fourthBitSpecified : 1;
unsigned int paddingBits : 4;
uint8_t oneByteStructValue;
uint16_t twoByteStructValue;
};
} U;
int main (void)
{
const char border[] = "==============================\n";
U myUnion;
uint8_t byte;
uint32_t fourBytes;
uint8_t i;
myUnion.fourByteValue = 0x00FFFFFF;
fourBytes = myUnion.fourByteValue; /* 1. This is not type-punning. */
printf("No type-punning fourByteValue:\n%s"
"fourBytes\t= 0x%.4x\n\n", border, fourBytes);
printf("Type-punning byteValue:\n%s", border);
for (i = 0; i < NUM_BYTES; i++)
{
byte = myUnion.byteValue[i]; /* 2. Type-punning allowed by C99,
no unspecified values. */
printf ("byte[%d]\t\t= 0x%.2x\n", i, byte);
}
printf("\n");
myUnion.byteValue[3] = 0xff;
fourBytes = myUnion.fourByteValue; /* 3. Type-punning allowed by C99
but all other 'byteValue's
are now unspecified values. */
printf("Type-punning fourByteValue:\n%s"
"fourBytes\t= 0x%.4x\n\n", border, fourBytes);
myUnion.firstBitSpecified = 0;
myUnion.thirdBitSpecified = 0;
fourBytes = myUnion.fourByteValue; /* 4. Again, this would be allowed, but
the bit that was just assigned
a value of 0 is implementation
defined AND all other bits are
unspecified values. */
printf("Type-punning firstBitSpecified:\n%s"
"fourBytes\t= 0x%.4x\n\n", border, fourBytes);
myUnion.fourByteValue = 0x00000001;
fourBytes = myUnion.firstBitSpecified; /* 5. Type-punning allowed, although
which bit you get is implementation
specific. */
printf("No type-punning, firstBitSpecified:\n%s"
"fourBytes\t= 0x%.4x\n\n", border, fourBytes);
fourBytes = myUnion.secondBitSpecified;
printf("No type-punning, secondBitSpecified:\n%s"
"fourBytes\t= 0x%.4x\n\n", border, fourBytes);
return (EXIT_SUCCESS);
}
The above code was compiled with mingw32-gcc.exe -Wall -g -std=c99
on a 64 bit Windows 7 machine. Upon running the code I receive the following output:
No type-punning fourByteValue:
==============================
fourBytes = 0xffffff
Type-punning byteValue:
==============================
byte[0] = 0xff
byte[1] = 0xff
byte[2] = 0xff
byte[3] = 0x00
Type-punning fourByteValue:
==============================
fourBytes = 0xffffffff
Type-punning firstBitSpecified:
==============================
fourBytes = 0xfffffffa
No type-punning, firstBitSpecified:
==============================
fourBytes = 0x0001
No type-punning, secondBitSpecified:
==============================
fourBytes = 0x0000