Hello, When I tried to link two modules which were compiled with different c++ standards, I observed that the offset of some fields of struct were different when the same struct was accessed from both the modules. The issue is due to the use of tail padding to allocate member variables in some standards (c++03, c++11) and not with others (c++14, c++17). I created a small program to reproduce the behaviour. -----------------------------padding_error.cpp---------------------------- #include <iostream> #include <stdint.h> #include <cstddef> struct A { int a; uint64_t b; int c = -1; }; struct B : public A { int d; }; int main() { std::cout << "sizeof(A): " << sizeof(A); std::cout << ", sizeof(B): " << sizeof(B) << std::endl; std::cout << "offset of d in B: " << (int)offsetof(B, d) << std::endl; } --------------------------------------------------------------------------
The output of this program depends on the -std flag. ~$ /opt/rh/devtoolset-8/root/bin/g++ -std=c++03 padding_error.cpp -o c03 ~$ ./c03 sizeof(A): 24, sizeof(B): 24 offset of d in B: 20 ~$ /opt/rh/devtoolset-8/root/bin/g++ -std=c++11 padding_error.cpp -o c11 ~$ ./c11 sizeof(A): 24, sizeof(B): 24 offset of d in B: 20 ~$ /opt/rh/devtoolset-8/root/bin/g++ -std=c++14 padding_error.cpp -o c14 ~$ ./c14 sizeof(A): 24, sizeof(B): 32 offset of d in B: 24 ~$ /opt/rh/devtoolset-8/root/bin/g++ -std=c++17 padding_error.cpp -o c17 ~$ ./c17 sizeof(A): 24, sizeof(B): 32 offset of d in B: 24 I tried with different versions of gcc but the output is the same. Because of the difference in the offset of the fields, I cannot link files which were compiled with different -std flags. I was not able to find anything relevant about this on the internet. Is this an expected behaviour? If so are there any flags which would allow to control this behavior and link files with different -std version together? Regards, Nayan Deshmukh