public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Query regarding unusual behaviour for tail padding with different c++ standards
       [not found] <CGME20211213050246epcms1p5b2dfcc034540c005f114473a8f94d94e@epcms1p5>
@ 2021-12-13  5:02 ` Nayan Deshmukh
  2021-12-13  5:14   ` Andrew Pinski
  0 siblings, 1 reply; 4+ messages in thread
From: Nayan Deshmukh @ 2021-12-13  5:02 UTC (permalink / raw)
  To: gcc; +Cc: Sung-Taek Lim, Yash Sharma

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

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

* Re: Query regarding unusual behaviour for tail padding with different c++ standards
  2021-12-13  5:02 ` Query regarding unusual behaviour for tail padding with different c++ standards Nayan Deshmukh
@ 2021-12-13  5:14   ` Andrew Pinski
  2021-12-13 10:34     ` Jonathan Wakely
       [not found]     ` <CGME20211213050246epcms1p5b2dfcc034540c005f114473a8f94d94e@epcms1p3>
  0 siblings, 2 replies; 4+ messages in thread
From: Andrew Pinski @ 2021-12-13  5:14 UTC (permalink / raw)
  To: n.deshmukh; +Cc: gcc, Sung-Taek Lim, Yash Sharma

On Sun, Dec 12, 2021 at 9:04 PM Nayan Deshmukh via Gcc <gcc@gcc.gnu.org> wrote:
> #include <iostream>
> #include <stdint.h>
> #include <cstddef>
> struct A {
>   int a;
>   uint64_t b;
>   int c = -1;
> };

The question becomes is the above a standard layout class or not. I
Noticed clang does not change the rules for layout between C++11 and
C++14 but GCC does.
I don't know the exact rules in the ABI to help you there but I do
think you should file a bug because it definitely looks unexpected
really.

Thanks,
Andrew Pinski

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

* Re: Query regarding unusual behaviour for tail padding with different c++ standards
  2021-12-13  5:14   ` Andrew Pinski
@ 2021-12-13 10:34     ` Jonathan Wakely
       [not found]     ` <CGME20211213050246epcms1p5b2dfcc034540c005f114473a8f94d94e@epcms1p3>
  1 sibling, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2021-12-13 10:34 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: n.deshmukh, gcc, Sung-Taek Lim, Yash Sharma

On Mon, 13 Dec 2021 at 05:14, Andrew Pinski wrote:
>
> On Sun, Dec 12, 2021 at 9:04 PM Nayan Deshmukh via Gcc <gcc@gcc.gnu.org> wrote:
> > #include <iostream>
> > #include <stdint.h>
> > #include <cstddef>
> > struct A {
> >   int a;
> >   uint64_t b;
> >   int c = -1;
> > };
>
> The question becomes is the above a standard layout class or not. I
> Noticed clang does not change the rules for layout between C++11 and
> C++14 but GCC does.
> I don't know the exact rules in the ABI to help you there but I do
> think you should file a bug because it definitely looks unexpected
> really.

The C++14 behaviour started with https://gcc.gnu.org/r216750

   Implement N3653 (Member initializers and aggregates) and fix
references to 'this' in constexpr cons
tructors.

           Implement N3653 (Member initializers and aggregates) and fix
           references to 'this' in constexpr constructors.
           * class.c (check_field_decls): In C++14 an NSDMI does not make the
           class non-aggregate.

But I think the layout should be using "POD for the purpose of
layout", which means the C++98 POD rules. Please do report it to
bugzilla.

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

* RE: Re: Query regarding unusual behaviour for tail padding with different c++ standards
       [not found]     ` <CGME20211213050246epcms1p5b2dfcc034540c005f114473a8f94d94e@epcms1p3>
@ 2021-12-13 12:28       ` Nayan Deshmukh
  0 siblings, 0 replies; 4+ messages in thread
From: Nayan Deshmukh @ 2021-12-13 12:28 UTC (permalink / raw)
  To: Jonathan Wakely, Andrew Pinski; +Cc: gcc, Sung-Taek Lim, Yash Sharma

> On Mon, 13 Dec 2021 at 05:14, Andrew Pinski wrote:
> >
> > On Sun, Dec 12, 2021 at 9:04 PM Nayan Deshmukh via Gcc <gcc@gcc.gnu.org> wrote:
> > > #include <iostream>
> > > #include <stdint.h>
> > > #include <cstddef>
> > > struct A {
> > >   int a;
> > >   uint64_t b;
> > >   int c = -1;
> > > };
> >
> > The question becomes is the above a standard layout class or not. I
> > Noticed clang does not change the rules for layout between C++11 and
> > C++14 but GCC does.
> > I don't know the exact rules in the ABI to help you there but I do
> > think you should file a bug because it definitely looks unexpected
> > really.
>  
> The C++14 behaviour started with https://gcc.gnu.org/r216750
>  
>    Implement N3653 (Member initializers and aggregates) and fix
> references to 'this' in constexpr cons
> tructors.
>  
>            Implement N3653 (Member initializers and aggregates) and fix
>            references to 'this' in constexpr constructors.
>            * class.c (check_field_decls): In C++14 an NSDMI does not make the
>            class non-aggregate.
>  
> But I think the layout should be using "POD for the purpose of
> layout", which means the C++98 POD rules. Please do report it to
> bugzilla.
 
I file a bug for this issue in bugzilla: 
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103681

Thanks,
Nayan

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

end of thread, other threads:[~2021-12-13 12:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20211213050246epcms1p5b2dfcc034540c005f114473a8f94d94e@epcms1p5>
2021-12-13  5:02 ` Query regarding unusual behaviour for tail padding with different c++ standards Nayan Deshmukh
2021-12-13  5:14   ` Andrew Pinski
2021-12-13 10:34     ` Jonathan Wakely
     [not found]     ` <CGME20211213050246epcms1p5b2dfcc034540c005f114473a8f94d94e@epcms1p3>
2021-12-13 12:28       ` Nayan Deshmukh

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).