Jump to content
  • Advertisement
Sign in to follow this  
YellowMaple

Unions in C

This topic is 4592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am somewhat familiar with the concept of unions in C, i.e. it's declared similar to a struct and the variables defined within share the same memory space: ex. union exampleUnion{ int i, j; char ch[ 4 ]; }; However, and I ask because I read this in a game design tutorial, that you can use unions instead of bit packing since bit packing can be somewhat error prone. In the example they declare a union like this: union uflags{ int flag1 : 1; int flag2 : 1; int flag3 : 1; }; and then just do something like uflags uf; if(uf.flag1) // stuff.. if(uf.flag2) // stuff.. What I don't get is what does the " : 1; " do when declaring "int flag1: 1;" inside a union? Plus if they all share the same memory space and each flag only is represented by a small number of bits, how can you do something like "if(uf.flag1)" and expect it to work without bit masking?

Share this post


Link to post
Share on other sites
Advertisement
Blech. I have never seen anyone use bitfield unions like that. Much more common is a struct bitfield, such as
struct uflags{
int flag1 : 1;
int flag2 : 1;
int flag3 : 1;
};
... which makes flag1, flag2, and flag3 each one-bit numbers. Using a union there makes them all the same one-bit number. I have no idea why anyone would ever want to do that.

Share this post


Link to post
Share on other sites
The only reason bitfields may be "error prone" is depending on how the compiler pads the data. Typically, it's better to align data on dwords. So for example, if you have a struct containing a total of 8 bits, it's possible the compiler gives it a size of 4 bytes, instead of the expected 1 byte. You would then need a #pragma to disable this padding for alignment.

Share this post


Link to post
Share on other sites
I agree with Sneftel. Bitfields in a union are an oddity.

A union comes in handy when a particular kind of data needs to be represented in different ways. For example, say you want to peform peform some pointer arithmetic on a void pointer. How is the compiler supposed to know the size of the data that the pointer points to? The result of sizeof(void) is undefined. The void pointer can easily be cast as a pointer to some type for one line, but if there are many lines of pointer arithmetic all that casting can make code messy (difficult to read and difficult to debug). A union can help to resolve that issue. For example:

typedef union tagMyType_u {
void *pvoid;
unsigned char *puchar;
unsigned long *pulong;
unsigned long ulong;
}
MyType_u;

MyType_u myvar;
myvar.pvoid = GetABlockOfMemory(...);

// get a pointer to the 10th dword value in the memory
unsigned long *pulong = myvar.pulong + 10;
// get the 10th dword value in the memory
unsigned long longvar = myvar.pulong[10];

// get a pointer to the 10th char value in the memory
unsigned char *puchar = myvar.puchar + 10;
// get the 10th char value in the memory
unsigned char charvar = myvar.puchar[10];

// get the address 10 bytes into the memory
unsigned long address = myvar.ulong + 10;
// this is the same address that puchar points to

If you want to examine a "really crazy" use of a union, check out the union at the heart of the MFC message pump, union MessageMapFunctions: Meandering Through the Maze of MFC Message and Command Routing.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The OP posted:

union exampleUnion{
int i, j;
char ch[ 4 ];
};


I hope you remember that ints aren't always 4-byte.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!