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: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Erick Ochoa <erick dot ochoa at theobroma-systems dot com>
- Cc: Richard Biener <richard dot guenther at gmail 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: Mon, 6 Jan 2020 11:03:18 +0000
- 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>
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.