0

I was looking at several SO posts on bit fields like link1 link2. I am using TDM-GCC-64 compiler. In wrote this code below to test the size of structures under various scenario.

#include <stdio.h>

struct P
{
    int k:2;
    char c:1;
};

struct Q
{
    int k:2;
    short c:1;
};

#pragma pack(1)
struct R
{
    int k:2;
    char c:1;
    int a;
};
#pragma pack()


struct M {
    int m;
    short l;
    short k;
    short x;
    short n:2;
    char a:2;
    char c:2;
};

struct N {
    int m:2;
    short l;
    short k;
    short x;
    short n:2;
    char a:2;
    char c:2;
};


int main()
{
    printf("sizeof struct P = %d\n",sizeof(struct P));
    printf("sizeof struct Q = %d\n",sizeof(struct Q));
    printf("sizeof struct R = %d\n",sizeof(struct R));
    printf("sizeof struct M = %d\n",sizeof(struct M));
    printf("sizeof struct N = %d\n",sizeof(struct N));
    return 0;
}

I am getting the sizeof(struct P) and sizeof(struct Q) as 8 bytes. This means that, character and integers will be considered separately and char will not be placed in integer space even if integer is of bit field type and free space is available. Again entire structure is aligned to the boundary of sizeof(int) and hence total size would be 4+1+3 padding bytes in case of P. If I use #pragma pack(1), I get 9 bytes and integers are now not naturally aligned and also entire structure is packed an not aligned to the boundary of integer type.

Considering the same concept, I was expecting sizeof(struct N) to be [4 byte for int, + 2 byte short + 2 byte short + 2 byte short + 2 byte for short + 1 byte for two char type combined + 3 for alignment = 16 bytes.

But sizeof(struct N) is 12bytes and not 16 bytes. Why size of M and N are not same?

Rajesh
  • 1,085
  • 1
  • 12
  • 25
  • 5
    The purpose of a bitfield is allow you to name separate ranges of bits within *one* variable. Using both `int` and `char` for 3-bits or `int` and `short` for 3-bits -- defeats the purpose. – David C. Rankin Mar 21 '18 at 00:57
  • I get size `16` for both `struct M` and `struct N` (MSVC). If I pack `struct N` it is size `13`. – Weather Vane Mar 21 '18 at 01:03
  • In the `struct M`, if you wanted to assign two bits to n, a and c, try making them all the same type. – bruceg Mar 21 '18 at 01:04
  • The C standard gives compilers a great deal of flexibility in how they implement bit fields. So there's not much to be said about this, except as others have pointed out, there's little to no benefit using different types for your bit fields. – user3386109 Mar 21 '18 at 01:12
  • My purpose is definitely not to combine few bits from int and few bits from char into one variable. struct P,Q and R are given just show the result in my compiler. I understand that structure size depends on compiler. But within the same compiler, why size of M and N are different is what my question. Within same compiler, behavior should be consistent right? – Rajesh Mar 21 '18 at 01:34
  • The behaviour is consistent if M is always handled the same way and N always the same way, but they need not be equal. However, for me and gcc they are. What is your gcc version and what does the compile command look like? – Arndt Jonasson Mar 21 '18 at 14:44
  • @ArndtJonasson Compiler version: tdm64-gcc-5.1.0-2 Compiler log: gcc.exe -Os -pedantic -Wextra -Wall -std=c99 -c C:\Temp\temp.c -o C:\Temp\temp.o g++.exe -o C:\Temp\temp.exe C:\Temp\temp.o – Rajesh Mar 21 '18 at 16:50
  • `-Os` means optimize for space, I think. – Arndt Jonasson Mar 21 '18 at 19:18

0 Answers0