C++类内存模型和对齐

发布于 2024-11-26 23:37:26 字数 242 浏览 2 评论 0原文

我有几个与 C++ 中的数据位置和对齐相关的问题要问。类是否具有与结构相同的内存放置和内存对齐格式?

更具体地说,数据是否根据声明的顺序加载到内存中?函数是否影响内存对齐和数据位置,或者它们是否分配到另一个位置?一般来说,我将所有内存对齐和位置相关的内容(例如文件头和算法数据)保留在结构中。我只是想知道这是否是类所固有的,就像结构一样,以及如果我选择使用这种方法,它是否可以很好地转换为类。

编辑:感谢您的所有回答。他们确实帮了很多忙。

I have several questions to ask that pertains to data position and alignment in C++. Do classes have the same memory placement and memory alignment format as structs?

More specifically, is data loaded into memory based on the order in which it's declared? Do functions affect memory alignment and data position or are they allocated to another location? Generally speaking, I keep all of my memory alignment and position dependent stuff like file headers and algorithmic data within a struct. I'm just curious to know whether or not this is intrinsic to classes as it is to structs and whether or not it will translate well into classes if I chose to use that approach.

Edit: Thanks for all your answers. They've really helped a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寂寞陪衬 2024-12-03 23:37:26

类是否具有相同的内存布局和内存对齐格式
作为结构?

对象的内存放置/对齐并不取决于其类型是声明为class还是struct。 C++ 中的 classstruct 之间的唯一区别是,类默认具有 private 成员,而 struct 具有 public< /code> 默认成员。

更具体地说,数据是否按照顺序加载到内存中
它声明了什么?

我不确定你所说的“加载到内存中”是什么意思。然而,在对象内,编译器不允许重新排列变量。例如:

class Foo {
    int a;
    int b;
    int c;
};

中,变量 c 必须位于 b 之后,b 必须位于 a 之后>Foo 对象。当创建 Foo 时,它们也按照类声明中显示的顺序构造(初始化),并在销毁 Foo 时以相反的顺序析构。

由于继承和访问修饰符,它实际上比这更复杂,但这是基本思想。

函数是否会影响内存对齐和数据位置?
分配到另一个位置?

函数不是数据,因此对齐不是它们所关心的问题。在某些可执行文件格式和/或体系结构中,函数二进制代码实际上占据与数据变量不同的区域,但 C++ 语言对此事实不可知。

一般来说,我保留所有的记忆对齐和位置
依赖的东西,如文件头和算法数据
结构。我只是想知道这是否是固有的
类与结构的关系以及它是否能很好地翻译
如果我选择使用这种方法,就可以进入课堂。

内存对齐几乎是编译器自动为您处理的事情。它更多的是一个实现细节。我说“几乎自动”,因为在某些情况下它可能很重要(序列化、ABI 等),但在应用程序中它不应该是一个问题。

关于读取文件(因为您提到了文件头),听起来您正在将文件直接读取到 struct 占用的内存中。我不推荐这种方法,因为填充和对齐问题可能会使您的代码在一个平台上运行,而不是在另一个平台上运行。相反,您应该从文件中一次读取几个原始字节,并通过简单的赋值将它们分配到 struct 中。

Do classes have the same memory placement and memory alignment format
as structs?

The memory placement/alignment of objects is not contingent on whether its type was declared as a class or a struct. The only difference between a class and a struct in C++ is that a class have private members by default while a struct have public members by default.

More specifically, is data loaded into memory based on the order in
which it's declared?

I'm not sure what you mean by "loaded into memory". Within an object however, the compiler is not allowed to rearrange variables. For example:

class Foo {
    int a;
    int b;
    int c;
};

The variables c must be located after b and b must be located after a within a Foo object. They are also constructed (initialized) in the order shown in the class declaration when a Foo is created, and destructed in the reverse order when a Foo is destroyed.

It's actually more complicated than this due to inheritance and access modifiers, but that is the basic idea.

Do functions affect memory alignment and data position or are they
allocated to another location?

Functions are not data, so alignment isn't a concern for them. In some executable file formats and/or architectures, function binary code does in fact occupy a separate area from data variables, but the C++ language is agnostic to that fact.

Generally speaking, I keep all of my memory alignment and position
dependent stuff like file headers and algorithmic data within a
struct. I'm just curious to know whether or not this is intrinsic to
classes as it is to structs and whether or not it will translate well
into classes if I chose to use that approach.

Memory alignment is something that's almost automatically taken care of for you by the compiler. It's more of an implementation detail than anything else. I say "almost automatically" since there are situations where it may matter (serialization, ABIs, etc) but within an application it shouldn't be a concern.

With respect with reading files (since you mention file headers), it sounds like you're reading files directly into the memory occupied by a struct. I can't recommend that approach since issues with padding and alignment may make your code work on one platform and not another. Instead you should read the raw bytes a couple at a time from the file and assign them into the structs with simple assignment.

筱果果 2024-12-03 23:37:26

类是否具有与结构相同的内存放置和内存对齐格式?

是的。从技术上讲,类和结构之间没有区别。唯一的区别是默认的成员访问规范,否则它们是相同的。

更具体地说,数据是否根据声明的顺序加载到内存中?

是的。

函数是否影响内存对齐和数据位置,或者它们是否分配到其他位置?

不会。它们不会影响对齐。方法是单独编译的。该对象不包含任何对方法的引用(对于那些说虚拟表确实影响成员的人来说,答案是是和否,但这是一个实现细节,不会影响成员之间的相对差异。允许编译器添加特定于实现的数据到对象)。

一般来说,我将所有内存对齐和位置相关的内容(例如文件头和算法数据)保留在结构中。

好的。不确定这会如何影响任何事情。

我只是想知道这是否是类所固有的,因为它是结构的

为同一事物构造类/结构的不同名称。

以及如果我选择使用这种方法,它是否能很好地转化为类。

选择什么方法?

Do classes have the same memory placement and memory alignment format as structs?

Yes. Technically there is no difference between a class and a struct. The only difference is the default member access specification otherwise they are identical.

More specifically, is data loaded into memory based on the order in which it's declared?

Yes.

Do functions affect memory alignment and data position or are they allocated to another location?

No. They do not affect alignment. Methods are compiled separately. The object does not contain any reference to methods (to those that say virtual tables do affect members the answer is yes and no but this is an implementation detail that does not affect the relative difference between members. The compiler is allowed to add implementation specific data to the object).

Generally speaking, I keep all of my memory alignment and position dependent stuff like file headers and algorithmic data within a struct.

OK. Not sure how that affects anything.

I'm just curious to know whether or not this is intrinsic to classes as it is to structs

Class/Structs different name for the same thing.

and whether or not it will translate well into classes if I chose to use that approach.

Choose what approach?

流心雨 2024-12-03 23:37:26

C++ 类简单地转换为结构体,其中所有实例变量作为结构体中包含的数据,而所有函数都与类分离,并被视为接受这些结构体作为参数的函数。

实例变量的确切存储方式取决于所使用的编译器,但它们通常是按顺序排列的。

C++ classes simply translate into structs with all the instance variables as the data contained inside the structs, while all the functions are separated from the class and are treated like functions with accept those structs as an argument.

The exact way instance variables are stored depends on the compiler used, but they generally tend to be in order.

﹉夏雨初晴づ 2024-12-03 23:37:26

C++ 类不参与“持久性”,如二进制模式结构,并且不应附加对齐方式。保持课程简单。
与类保持一致可能会产生负面的性能优势,并且也可能产生副作用。

C++ classes do not participate in "persistence", like binary-mode structures, and shouldn't have alignment attached to them. Keep the classes simple.
Putting alignment with classes may have negative performance benefits and may have side effects too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文