C ++类内存模型和对齐方式 [英] C++ Class Memory Model And Alignment

查看:69
本文介绍了C ++类内存模型和对齐方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要问几个问题,这些问题与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.

感谢您的所有回答.他们确实提供了很多帮助.

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

推荐答案

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

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

对象的内存放置/对齐方式并不取决于其类型是声明为class还是struct.在C ++中,classstruct之间的唯一区别是,类默认情况下具有private成员,而结构默认情况下具有public成员.

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;
};

Foo对象中,变量c必须位于b之后,而b必须位于a之后.在创建Foo时,它们也按照类声明中显示的顺序进行构造(初始化),并在破坏Foo时以相反的顺序对其进行破坏.

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?

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

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.

内存对齐几乎是编译器自动为您处理的.最重要的是实现细节.我说几乎自动"是因为在某些情况下它可能很重要(序列化,ABI等),但是在应用程序内就不必担心了.

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.

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

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.

这篇关于C ++类内存模型和对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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