Jump to content
  • Advertisement
Sign in to follow this  
xyuri

typedef union size ...

This topic is 4609 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

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
Advertisement
I realise the int is 4 bytes, but i changed it to char and still the same thing... it seems to be the bit field that is causing the extra 3 bytes.

Share this post


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

ace

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
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!