0

What are the consequences of defining a structure as follows:

typedef struct {
    bool          Bit0 : 1;    //Bit 0
    bool          Bit1 : 1;
    bool          Bit2 : 1;
    bool          Bit3 : 1;
    bool          Bit4 : 1;
    bool          Bit5 : 1;
    bool          Bit6 : 1;
    bool          Bit7 : 1;     //Bit 7
    char          SomeOtherData;
}Char16Bits;

...

Char16Bits MyStructure;

MyStructure.Bit0 = true;
MyStructure.Bit1 = false;

In my test program everything seems fine, each of the "Bit0-7" only occupies 1 bit and I can see them in memory working as expected. The "SomeOtherData" char appears to be the second byte of the structure in memory when looking at the VS2010 Memory window which is all good.

However, can I reliably assume that each of these bits will always only occupy 1 bit? If I memcpy 2 bytes into this structure, will the 2nd byte always reliably occupy the "SomeOtherData" char element in my structure?

Barmar
  • 741,623
  • 53
  • 500
  • 612
ALM865
  • 1,078
  • 13
  • 21
  • Have you tried checking sizeof(Char16Bits)? – phyrrus9 May 23 '14 at 01:22
  • 1
    you can `static_assert` with `offsetof` – Bryan Chen May 23 '14 at 01:23
  • @Bryan Chen, so are you suggesting throwing an error if the compiler creates the structure with more than 16bits? – ALM865 May 23 '14 at 01:28
  • @phyrrus9, sizeof doesn't always work since there are sometimes extra bytes added to the end of structures depending on what you've declared. – ALM865 May 23 '14 at 01:29
  • 2
    One consequence is that any of `Bit0` and `Bit7` may not be modified concurrently with another in the same `Char16Bits` object by two or more threads. With ordinary `bool` fields, this isn't a problem. – Brian Bi May 23 '14 at 01:30
  • @Brian, that's a very good point! I am using threads and that situation could have easily arose. – ALM865 May 23 '14 at 01:35

1 Answers1

4

Structure packing is always compiler-dependent, but most compilers should put this in two bytes.

You can rely on sizeof(MyStructure) being the total size of the data structure (taking into account padding as discussed here Why isn't sizeof for a struct equal to the sum of sizeof of each member?), and offsetof(Char16Bits,SomeOtherData) being the correct offset of SomeOtherData.

If you need to write code that assumes particular sizes or offsets, use assert() or static_assert() so that the code isn't allowed to run on platforms that don't follow your assumptions.

Community
  • 1
  • 1
Russell Borogove
  • 18,516
  • 4
  • 43
  • 50
  • Thanks for the answer, I suspected as much. Another question, once the application is compiled is there any possible way that the size of the structure will change? I.e. if it compiles and runs fine on my PC is there any possible way that same executable will allocate the structure differently on another PC (assuming you are using the exact same executable)? – ALM865 May 23 '14 at 01:34
  • No, the size of a structure is nailed down at compile time. – Russell Borogove May 23 '14 at 01:41
  • Thanks heaps @Russell Borogrove! That's answered my question exactly :) – ALM865 May 23 '14 at 01:42