This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]