public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* struct and members addresses
@ 2003-04-24 11:01 Cédric Lucantis
  2003-04-24 11:15 ` Alexandre Courbot
  2003-04-24 11:29 ` Andrea 'fwyzard' Bocci
  0 siblings, 2 replies; 6+ messages in thread
From: Cédric Lucantis @ 2003-04-24 11:01 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 624 bytes --]

Hello

   I would like to know if I can assume that a structure and its first member
will always have the same address. For instance, with the following code:

/* ----------------------------------*/

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' ?

thanks


______________________________________________________
Plus simple, plus fiable, plus rapide : Découvrez le nouveau Caramail - http://www.caramail.com



^ permalink raw reply	[flat|nested] 6+ messages in thread
* RE: struct and members addresses
@ 2003-04-24 11:18 Agust Karlsson
  0 siblings, 0 replies; 6+ messages in thread
From: Agust Karlsson @ 2003-04-24 11:18 UTC (permalink / raw)
  To: Alexandre Courbot, Cédric Lucantis, gcc-help

Or to be sure you could use
__attribute__ ((packed)) 

I.e.
	 struct foo
	 {
	     any_type    first_member;
	 } __attribute__ ((packed)) ;

	Gústi
--
Agust Karlsson            mailto:gusti@pallas.dk
Pallas Informatik A/S     http://www.pallas.dk
Allerød Stationsvej 2D    Tel.: +45 48 10 24 10
DK-3450 Allerød           Fax.: +45 48 10 24 01


> -----Original Message-----
> From:	Alexandre Courbot [SMTP:Alexandre.Courbot@lifl.fr]
> Sent:	Thursday, April 24, 2003 1:16 PM
> To:	Cédric Lucantis; gcc-help@gcc.gnu.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.
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread
* RE: struct and members addresses
@ 2003-04-24 12:58 Martin York
  0 siblings, 0 replies; 6+ messages in thread
From: Martin York @ 2003-04-24 12:58 UTC (permalink / raw)
  To: gcc-help



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.Courbot@lifl.fr]
Sent: 24 April 2003 07:16
To: Cédric Lucantis; gcc-help@gcc.gnu.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.

^ permalink raw reply	[flat|nested] 6+ messages in thread
[parent not found: <616BE6A276E3714788D2AC35C40CD18DC3E972@whale.softwire.co.uk>]

end of thread, other threads:[~2003-04-24 13:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-04-24 11:01 struct and members addresses Cédric Lucantis
2003-04-24 11:15 ` Alexandre Courbot
2003-04-24 11:29 ` Andrea 'fwyzard' Bocci
2003-04-24 11:18 Agust Karlsson
2003-04-24 12:58 Martin York
     [not found] <616BE6A276E3714788D2AC35C40CD18DC3E972@whale.softwire.co.uk>
2003-04-24 13:25 ` Rupert Wood

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).