473,491 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

access specifiers and memory layout

I just read something interesting in one of the PDFs located here:
http://www.cs.wustl.edu/~schmidt/C++/ Sorry, I don't recall which file it
was, and I'm too lazy to dig it up again ;) It says that the compiler is
obligated to arrange the memory of this class in declaration order:

class InOrder{
public:
int a;
int b;
int c;
};

But a compiler is free to rearrange this:

class AnyOrder{
public: int a;
public: int b;
public: int c;
};

Anybody know about this?

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #1
10 1824
Steven T. Hatton wrote:
I just read something interesting in one of the PDFs located here:
http://www.cs.wustl.edu/~schmidt/C++/ Sorry, I don't recall which file it
was, and I'm too lazy to dig it up again ;) It says that the compiler is
obligated to arrange the memory of this class in declaration order:

class InOrder{
public:
int a;
int b;
int c;
};

But a compiler is free to rearrange this:

class AnyOrder{
public: int a;
public: int b;
public: int c;
};

Anybody know about this? never heard about that, as far as i know those two are the same,

G


--
http://www.kolumbus.fi/bob.smith
Jul 23 '05 #2
The allocation for member variables, which are not separated by a
different access specifier, will be in the same order as their
declaration in the class; this is to provide compatibility with C code.
In the above example, the memory layout of an object of InOrder will be
contiguous for a, b, c.
Moreover, if we have protected and also private members, the memory
layout can reorder/group the member variables as follows, irrespective
of their declaration order in the class:

memory layout:
int a, b, c; // public members first
int d, e, f; // protected
int h, i, j; // private in the last.

this is to provide minimal recompilation to the client code whenever
there is any change to the private part/data of the class used.

This is not the only method the compiler follows but this is one of the
many tricks.

Jul 23 '05 #3
Bob Smith wrote:
Steven T. Hatton wrote:
I just read something interesting in one of the PDFs located here:
http://www.cs.wustl.edu/~schmidt/C++/ Sorry, I don't recall which file it
was, and I'm too lazy to dig it up again ;) It says that the compiler is
obligated to arrange the memory of this class in declaration order:

class InOrder{
public:
int a;
int b;
int c;
};

But a compiler is free to rearrange this:

class AnyOrder{
public: int a;
public: int b;
public: int c;
};

Anybody know about this?

never heard about that, as far as i know those two are the same,

G

It looks like Schmidt's right, unless the specification has changed. See
TC++ARM §9.2 page 173.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #4
Steven T. Hatton wrote:
It looks like Schmidt's right, unless the specification has changed. See
TC++ARM §9.2 page 173.


ARM is a bit outdated.
Look up in the standard:
§11.1.2.
and
§9.2.12.

Jul 23 '05 #5
leonardo77 wrote:
Steven T. Hatton wrote:
It looks like Schmidt's right, unless the specification has changed. See
TC++ARM §9.2 page 173.


ARM is a bit outdated.
Look up in the standard:
§11.1.2.
and
§9.2.12.


So the specification hasn't changed in this regard.

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #6
"Steven T. Hatton" <ch********@germania.sup> wrote in message
news:nd********************@speakeasy.net
leonardo77 wrote:
Steven T. Hatton wrote:
It looks like Schmidt's right, unless the specification has
changed. See TC++ARM §9.2 page 173.


ARM is a bit outdated.
Look up in the standard:
§11.1.2.
and
§9.2.12.


So the specification hasn't changed in this regard.

I think that 9.2.14 also needs to be looked at. It suggests to me that your
InOrder and AnyOrder must be layout compatible. Alas, the standard doesn't
seem to give a clear statement of what layout-compatible means. As a
practical matter, I would be amazed if any compiler laid the two structs out
in a different order, given that the access specifiers are really a
degenerate case.

--
John Carson

Jul 23 '05 #7
John Carson wrote:
"Steven T. Hatton" <ch********@germania.sup> wrote in message
news:nd********************@speakeasy.net
leonardo77 wrote:
Steven T. Hatton wrote:
It looks like Schmidt's right, unless the specification has
changed. See TC++ARM §9.2 page 173.

ARM is a bit outdated.
Look up in the standard:
§11.1.2.
and
§9.2.12.


So the specification hasn't changed in this regard.

I think that 9.2.14 also needs to be looked at. It suggests to me that
your InOrder and AnyOrder must be layout compatible. Alas, the standard
doesn't seem to give a clear statement of what layout-compatible means. As
a practical matter, I would be amazed if any compiler laid the two structs
out in a different order, given that the access specifiers are really a
degenerate case.


I too would be quite surprised if it made a difference. The only reason I
could see for that would be some kind of space optimization if the members
were of different type. It does, however, suggest that

class Klass{
public:
int a;
private:
int b;
public:
int c;
};

might get re-suffled.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #8
Steven T. Hatton wrote:
So the specification hasn't changed in this regard.


Sorry! I was a bit confused about your statement.

Jul 23 '05 #9
"Steven T. Hatton" <ch********@germania.sup> wrote in message
news:1M********************@speakeasy.net
John Carson wrote:

I think that 9.2.14 also needs to be looked at. It suggests to me
that your InOrder and AnyOrder must be layout compatible. Alas, the
standard doesn't seem to give a clear statement of what
layout-compatible means. As a practical matter, I would be amazed if
any compiler laid the two structs out in a different order, given
that the access specifiers are really a degenerate case.


I too would be quite surprised if it made a difference. The only
reason I could see for that would be some kind of space optimization
if the members were of different type. It does, however, suggest that

class Klass{
public:
int a;
private:
int b;
public:
int c;
};

might get re-suffled.

That is certainly a possibility.

--
John Carson
Jul 23 '05 #10
leonardo77 wrote:
Steven T. Hatton wrote:
So the specification hasn't changed in this regard.


Sorry! I was a bit confused about your statement.


Nothing to apologize for. You cleared up the matter. The Standard could
well have changed. I was simply communicating the fact to the rest of the
folks here.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1477
by: Terje Slettebø | last post by:
Well, as is customary when you're new to a group, you tend to post quite a bit, :) so here's one more. Some things I've been wondering about with PHP (5). Today, I worked on an implementation of...
35
2288
by: Gabriel Zachmann | last post by:
Is there any generic way to use C++ libraries from within Python. I seem to recall that there are tools to generate wrappers for C-libraries semi-automatically. But those were still way too...
1
1605
by: BDob | last post by:
Standard documents "Access control is not considered in determining overriding." To further explain what's puzzling me, I put up an example below: class Base { public: virtual void...
4
2818
by: Martin Vorbrodt | last post by:
say i have this: union U { public: U(int a) : x(a) {} protected: int x, y, z; }; why is the protected access even allowed for unions? no inheritance can take
6
6089
by: vipul DotNet | last post by:
hi, which all are the access specifiers in vb.net thank you vipul
8
2424
by: Tapeesh | last post by:
I have a following piece of code. The code was compiled using g++ class A { public : virtual void fn() = 0; }; class B: virtual private A {
11
4506
by: Henryk | last post by:
I have something like class Params { public: const static char nOne = 1; const static int nTwo = 2; const static char nThree = 3; }; This is just a wrapper for globally used parameters in...
24
1933
by: Kavya | last post by:
int main (){ int a={{1,2,3},{4,5,6}}; int (*ptr)=a; /* This should be fine and give 3 as output*/ printf("%d\n",(*ptr)); ++ptr;
9
4473
by: prakashwadhwani | last post by:
Hi !! I'm about to develop a new project for a client. Should I go about it in Access 2003 or 2007 ? Purchasing it either for me or for my client is not a major consideration here ... what I'd...
0
7115
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7154
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7190
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5451
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.