Question about sizeof after struct change

Richard Biener richard.guenther@gmail.com
Tue Jan 7 12:43:00 GMT 2020


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.



More information about the Gcc mailing list