public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug other/103681] New: Unusual behavior for tail padding with different c++ standards
@ 2021-12-13 12:24 n.deshmukh at samsung dot com
  2021-12-13 12:26 ` [Bug other/103681] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: n.deshmukh at samsung dot com @ 2021-12-13 12:24 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-05-13 15:27 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-13 12:24 [Bug other/103681] New: Unusual behavior for tail padding with different c++ standards n.deshmukh at samsung dot com
2021-12-13 12:26 ` [Bug other/103681] " pinskia at gcc dot gnu.org
2021-12-13 12:58 ` [Bug c++/103681] [9/10/11/12 Regression] Unusual behavior for tail padding with different c++ standards and NSDMI pinskia at gcc dot gnu.org
2021-12-13 18:55 ` jason at gcc dot gnu.org
2021-12-13 19:02 ` jason at gcc dot gnu.org
2021-12-14 18:43 ` jason at gcc dot gnu.org
2021-12-17  3:27 ` cvs-commit at gcc dot gnu.org
2021-12-17 20:42 ` [Bug c++/103681] [9/10/11 " jason at gcc dot gnu.org
2022-01-21 17:06 ` cvs-commit at gcc dot gnu.org
2022-05-13 15:27 ` jason at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).