/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
GS
15 Jan. 2022
See: https://stackoverflow.com/questions/70727668/bizarre-struct-member-packing-in-32-bit-clang
*******************************************************************************/
#include <iostream>
struct _8Bytes
{
uint64_t _8bytes;
};
struct _16Bytes : _8Bytes
{
uint32_t _4bytes;
};
struct _16Bytes2
{
uint64_t _8bytes;
uint32_t _4bytes;
};
struct Test : _16Bytes
{
uint8_t test;
};
struct Test2
{
uint64_t _8bytes;
uint32_t _4bytes;
uint8_t test;
};
struct Test3 : _16Bytes2
{
uint8_t test;
};
struct Test4
{
uint64_t _8bytes;
uint32_t _4bytes;
uint8_t padding[3]; // explicitly place 3 bytes of padding
uint8_t test;
};
struct __attribute__ ((__packed__)) Test5
{
uint8_t padding[3]; // explicitly place 3 bytes of padding
uint64_t _8bytes;
uint32_t _4bytes;
uint8_t test;
};
int main()
{
printf("sizeof(_8Bytes) = %lu\n", sizeof(_8Bytes));
printf("sizeof(_16Bytes) = %lu\n", sizeof(_16Bytes));
printf("sizeof(_16Bytes2) = %lu\n", sizeof(_16Bytes2));
printf("sizeof(Test) = %lu\n", sizeof(Test));
printf("sizeof(Test2) = %lu\n", sizeof(Test2));
printf("sizeof(Test3) = %lu\n", sizeof(Test3));
printf("sizeof(Test4) = %lu\n", sizeof(Test4));
printf("sizeof(Test5) = %lu\n", sizeof(Test5));
printf("\n");
printf("offsetof(Test, test) = %lu\n", offsetof(Test, test));
printf("offsetof(Test2, test) = %lu\n", offsetof(Test2, test));
printf("offsetof(Test3, test) = %lu\n", offsetof(Test3, test));
printf("offsetof(Test4, test) = %lu\n", offsetof(Test4, test));
printf("offsetof(Test5, test) = %lu\n", offsetof(Test5, test));
return 0;
}