Sign in to follow this  
xyuri

typedef union size ...

Recommended Posts

i have created the following union:
typedef union {
  int  i;
  struct {
		unsigned b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1, b8:1;
	};
} bla;
but when i check the size of it with the sizeof() function it returns 4, which is 4 bytes. How can this union be 4 bytes? What I wantd was a 1 byte structure. Does anyone know why this is, or a solution? Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by ace_lovegrove
All i know is that enough room is allocated for the size of the largest type.

ace


Yeah, thats what unions are for :) But why the bitfield of 8 bits is pushing it up to 32 bits is beyond me :(

Anyone?

Share this post


Link to post
Share on other sites
In this case it isn't to do with packing, i believe. Because the integer is declared first. If the 'char' was declared first then 24 bits might be wasted because the compiler is liberal with memory.

ace

Share this post


Link to post
Share on other sites
This will create a 1 byte union on my version of GCC:

typedef union {
char i;
struct {
unsigned char b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1, b8:1;
};
} bla;

Without the char infront of b1 it is 4 bytes.

Share this post


Link to post
Share on other sites
Quote:
Original post by Scet
This will create a 1 byte union on my version of GCC:

typedef union {
char i;
struct {
unsigned char b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1, b8:1;
};
} bla;

Without the char infront of b1 it is 4 bytes.


I'm using MingW which might be the reason... Good thing this is for an assignment and is meant to be compiled using GCC.

Even with all the compiler optimisations on or off doesnt make a diff, it is definately making the bitfield 32 bits long, which sucks :(

Anyways, thanks fella's, i dont think i will find a cure for this using my compiler :)

Share this post


Link to post
Share on other sites
There is no standard way to guarantee the exact size of a user-defined type in C++. Your types are just being padded to a 4 byte boundary even though their representations only need 1 byte. Many compilers have pragmas to toggle padding settings if you don't mind being nonportable.

Anyway, a more portable way would be to just use a char type directly and work with it using functions that treat it like a series of bits using bitwise operations. This way you are guaranteed to have a 1 byte type.

Share this post


Link to post
Share on other sites
Quote:
Original post by Polymorphic OOP
There is no standard way to guarantee the exact size of a user-defined type in C++. Your types are just being padded to a 4 byte boundary even though their representations only need 1 byte. Many compilers have pragmas to toggle padding settings if you don't mind being nonportable.

Anyway, a more portable way would be to just use a char type directly and work with it using functions that treat it like a series of bits using bitwise operations. This way you are guaranteed to have a 1 byte type.

Youre right... I'm just gonna use a char are use bit masks :)

Thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this