访问说明符和内存布局 [英] access specifiers and memory layout

查看:46
本文介绍了访问说明符和内存布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在其中一个PDF中阅读了一些有趣的内容:
http://www.cs.wustl.edu/~schmidt/C++/ 对不起,我不记得它是什么文件

是,我''我懒得再把它挖出来;)它说编译器是

有义务按照声明顺序安排这个类的内存:


class InOrder {

public:

int a;

int b;

int c;

};


但是编译器可以自由重新安排这个:


class AnyOrder {

public: int a;

public:int b;

public:int c;

};


有人知道吗?


-

如果我们的假设是关于任何事情而不是某个或多个特别是b $ b b事情,然后我们的推论构成数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题.- Bertrand Russell

解决方案

Steven T. Hatton写道:

我刚刚在其中一个PDF中阅读了一些有趣的内容:
http://www.cs.wustl.edu/~ schmidt / C ++ / 对不起,我不记得它是什么文件,而且我懒得再把它挖出来;)它说编译器是有义务的按声明顺序安排本课程的记忆:

课程InOrder {
public:
int a;
int b;
int c; <但是编译器可以自由地重新安排它:

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

有人知道吗?
从来没有听说过,据我所知,这两个是相同的,


G




- -
http://www.kolumbus.fi/bob.smith


成员变量的分配,不是由

不同的访问说明符分隔,将与它们的顺序相同br />
类中的声明;这是为了提供与C代码的兼容性。

在上面的例子中,InOrder对象的内存布局将为a,b,c连续的


此外,如果我们有受保护的也是私人成员,内存

布局可以按如下方式重新排序/分组成员变量,无论

他们的声明在课堂上订购:


内存布局:

int a,b,c; //公众成员首先

int d,e,f; //保护

int h,i,j; //最后是私有的。


这是为客户端代码提供最小的重新编译时

对私有部分/数据的任何更改使用的类。


这不是编译器遵循的唯一方法,但这是许多技巧之一。


Bob Smith写道:

Steven T. Hatton写道:

我刚读了一些有趣的东西其中一个PDF位于:
http:// www .cs.wustl.edu / ~schmidt / C ++ / 对不起,我不记得它是什么文件,而且我懒得再把它挖出来;)它说的那样编译器有义务按声明顺序排列此类的内存​​:

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

但是编译器可以自由重启ange this:

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

任何人都知道这个?


从来没有听说过,据我所知,这两个是相同的,

G



看起来施密特是正确的,除非规范发生了变化。参见

TC ++ARM§9.2第173页。

-

如果我们的假设是关于任何事情而不是关于某一个或多个

特定的东西,然后我们的推论构成数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题.- Bertrand Russell


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

解决方案

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


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.


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


这篇关于访问说明符和内存布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆