This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
RE: struct and members addresses
- From: Martin York <martin dot york at veritas dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Thu, 24 Apr 2003 08:58:35 -0400
- Subject: RE: struct and members addresses
Just to throw a spanner in the works!
Assuming C only
What happens if there are more members of the structure.
Is the compiler allowed to reorder the members of the
structure for efficiency in memory layout?
-----Original Message-----
From: Alexandre Courbot [mailto:Alexandre dot Courbot at lifl dot fr]
Sent: 24 April 2003 07:16
To: Cédric Lucantis; gcc-help at gcc dot gnu dot org
Subject: Re: struct and members addresses
> struct foo
> {
> any_type first_member;
> };
>
> struct foo my_struct;
>
> void * addr1 = &my_struct;
> void * addr2 = &my_struct.first_member;
>
> /* ----------------------------------*/
>
> can I be sure that `addr1' will be equal to `addr2' ?
In C, I think you can safely. In C++, if your struct has virtual methods,
the
adress won't be the same since I think GCC puts the vtable at the beginning
of the struct. But as long as you don't use virtuals, it should be ok AFAIK.
Alex.