Topic: Class Object Memory Layout


Author: s w <jjscwjunk@yahoo.com>
Date: 2000/06/30
Raw View
Consider the following example:

class Table {
public:
  int offset;
  int size;
}

Does the c++ standard guarantees the order of the two
members in memory(i.e., offset comes before size)?  or
is it implementation dependent?

Steve


__________________________________________________
Do You Yahoo!?
Get Yahoo! Mail - Free email you can access from anywhere!
http://mail.yahoo.com/

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Bill Wade" <bill.wade@stoner.com>
Date: 2000/06/30
Raw View
"s w" <jjscwjunk@yahoo.com> wrote in message
news:20000629164411.28356.qmail@web1604.mail.yahoo.com...
> Consider the following example:
>
> class Table {
> public:
>   int offset;
>   int size;
> }
>
> Does the c++ standard guarantees the order of the two
> members in memory(i.e., offset comes before size)?  or
> is it implementation dependent?

Yes.  The answer changes to No if you write an access specifier (such as
public:) between the two members.  However the Yes answer alone usually
isn't all that useful.  It would be helpful if you were guaranteed that no
padding occurred between the elements, so that &size == &offset+1.  I'm
pretty sure the standard doesn't make that promise, although it seems that
only perverse implementations would violate it.

See the recent "Set,Array,Union" thread in comp.std.c for more discussion
than you'd ever want to see.



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Roger Orr <roger_orr@my-deja.com>
Date: 2000/07/02
Raw View
In article <20000629164411.28356.qmail@web1604.mail.yahoo.com>,
  s w <jjscwjunk@yahoo.com> wrote:
> Consider the following example:
>
> class Table {
> public:
>   int offset;
>   int size;
> }
>
> Does the c++ standard guarantees the order of the two
> members in memory(i.e., offset comes before size)?

Yes the standard does guaranteee this:-

9.2 Class members [class.mem]
12 Nonstatic data members of a (non-union) class declared without an
intervening access-specifier are allocated so that later members have
higher addresses within a class object.

Roger.


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]