Why does the size of struct bit_man1 result in 8 bytes while struct bit_man2 results in 4 bytes, even though both structures contain the same types and amount of data? Consider the structures defined below:
#include <stdio.h>
// packing is done in 4 bytes size since int is the biggest in struct
struct bit_man1 {
int a:5; //5 bits
int b:8; //5+8 =13 bits
char abc[2]; //2 bytes 8 +8 +13= 29 bit
int z:2; // 2 bits 29+2 =31
};
struct bit_man2 {
int a:5; //5 bits
int b:8; //5+8 =13 bits
int z:2; // 2 bits 13+2=15
char abc[2]; //2 bytes 8 +8 +15= 31 bit
};
int main() {
printf("Size of bit_man1: %zu bytes\n", sizeof(struct bit_man1));
printf("Size of bit_man2: %zu bytes\n", sizeof(struct bit_man2));
return 0;
}
gcc version
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.4.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
But we get an ouput like so
Size of bit_man1: 8 bytes
Size of bit_man2: 4 bytes
as packing prioritizes 4 bytes should'nt the int z:2 (2 bits) be packed into the 29 bits resulting in 31 bits, is there a any reason why the compiler allocates another 4 bytes??
Adjacent bitfields can be grouped together into the same allocation unit, but bitfields and non-bitfields are not. This means that the
abcmember will take up 2 complete bytes and not occupy any partial bytes such as unused bit in a unit containing a bitfield.In the first case, you start with two bitfields totaling 13 bits. These fit into a single 2 byte allocation unit. The
chararray occupies the next 2 complete bytes. Then the remaining bitfield takes up another byte. Then since the base type of the bitfields isintthe struct is padded out another 3 bytes (assuming aninttakes up 4 bytes) for a total of 8.In the second case you start with three bitfields totaling 15 bits. These fit into a single 2 byte allocation unit as in the prior case. The
chararray occupies the next 2 complete bytes for a total of 4 bytes used.