[Bug other/103681] New: Unusual behavior for tail padding with different c++ standards

n.deshmukh at samsung dot com gcc-bugzilla@gcc.gnu.org
Mon Dec 13 12:24:23 GMT 2021


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103681

            Bug ID: 103681
           Summary: Unusual behavior for tail padding with different c++
                    standards
           Product: gcc
           Version: 8.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
          Assignee: unassigned at gcc dot gnu.org
          Reporter: n.deshmukh at samsung dot com
  Target Milestone: ---

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. 

The issue can be reproduced with all the gcc compilers which support c++14.


More information about the Gcc-bugs mailing list