public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* 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

* RE: struct and members addresses
       [not found] <616BE6A276E3714788D2AC35C40CD18DC3E972@whale.softwire.co.uk>
@ 2003-04-24 13:25 ` Rupert Wood
  0 siblings, 0 replies; 6+ messages in thread
From: Rupert Wood @ 2003-04-24 13:25 UTC (permalink / raw)
  To: 'Martin York'; +Cc: gcc-help

Martin York wrote: 

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

No. C99 6.2.5.20 second bullet - "sequentially allocated":

    A /structure type/ describes a sequentially allocated nonempty set
    of member objects (and, in certain circumstances, an incomplete
    array), each of which has an optionally specified name and
    possibly distinct type.

Minor exception for adjacent bit-fields combined into a single unit; 6.2.7.1
bullets 8 and 10:

    A member of a structure or union may have any object type other
    than a variably modified type. In addition, a member may be
    declared to consist of a specified number of bits (including a
    sign bit, if any). Such a member is called a /bit-field/; its
    width is preceded by a colon.

    An implementation may allocate any addressable storage unit large
    enough to hold a bit-field. If enough space remains, a bit-field
    that immediately follows another bit-field in a structure shall be
    packed into adjacent bits of the same unit. ... The order of
    allocation of bit-fields with a unit (high-order to low-order or
    low-order to high-order) is implementation defined. The alignment
    of the addressable storage unit is unspecified.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: 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
  1 sibling, 0 replies; 6+ messages in thread
From: Andrea 'fwyzard' Bocci @ 2003-04-24 11:29 UTC (permalink / raw)
  To: Cédric Lucantis; +Cc: gcc-help

At 13.00 24/04/2003 +0100, Cédric Lucantis wrote:
>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

I think so.
IF yours is a simple struct, ie. no hereditaritey or any C++ thing.

You can check that offsetof (my_struct, first_member) is 0.

.fwyzard. 

^ 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 11:01 Cédric Lucantis
@ 2003-04-24 11:15 ` Alexandre Courbot
  2003-04-24 11:29 ` Andrea 'fwyzard' Bocci
  1 sibling, 0 replies; 6+ messages in thread
From: Alexandre Courbot @ 2003-04-24 11:15 UTC (permalink / raw)
  To: Cédric Lucantis, gcc-help


> 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

* 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

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 12:58 struct and members addresses Martin York
     [not found] <616BE6A276E3714788D2AC35C40CD18DC3E972@whale.softwire.co.uk>
2003-04-24 13:25 ` Rupert Wood
  -- strict thread matches above, loose matches on Subject: below --
2003-04-24 11:18 Agust Karlsson
2003-04-24 11:01 Cédric Lucantis
2003-04-24 11:15 ` Alexandre Courbot
2003-04-24 11:29 ` Andrea 'fwyzard' Bocci

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).