This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Question about sizeof after struct change
- From: Richard Biener <richard dot guenther at gmail dot com>
- To: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- Cc: Erick Ochoa <erick dot ochoa at theobroma-systems dot com>, "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>, Christoph Müllner <christoph dot muellner at theobroma-systems dot com>, "Dr. Philipp Tomsich" <philipp dot tomsich at theobroma-systems dot com>
- Date: Tue, 7 Jan 2020 13:43:13 +0100
- Subject: Re: Question about sizeof after struct change
- References: <e409d1eb-ca51-61a3-8777-1f2ab92ba9b8@theobroma-systems.com> <E3576E8D-AC16-439F-9F4E-AF99052C8931@gmail.com> <1d686046-16b9-f8a6-178e-4bff501aaef2@theobroma-systems.com> <CAH6eHdQo0RzGo9iL6UpJPAHPeS-0Sw0zyR3oxRxJuCS7z9zhHQ@mail.gmail.com>
On Mon, Jan 6, 2020 at 12:03 PM Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>
> On Fri, 3 Jan 2020 at 16:09, Erick Ochoa wrote:
> > Do you mean something like taking the address of a struct and adding an offset
> > to access a field?
>
> Yes, the following code is valid:
>
> struct S { int unused; int used; };
> static_assert( sizeof(S) == 8 );
> S s[4];
> int* s2_used = (int*)((char*)s + 20);
>
> Accessing the field doesn't refer to S::used or S::unused directly,
> and doesn't use sizeof(S) or sizeof(int) explicitly.
>
> So if your pass removes S::unused, then you'd need to analyse
> ((char*)s + 20) to determine that it accesses s[2].used and then
> adjust it to ((char*)s + 8) instead.
Also GCC, at its own discretion, might decide to change
s[1].used to MEM[&s + 8] because semantically they are
the same.
Richard.