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.
Edit: Thanks for all your answers. They've really helped a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对象的内存放置/对齐并不取决于其类型是声明为
class
还是struct
。 C++ 中的class
和struct
之间的唯一区别是,类默认具有private
成员,而 struct 具有public< /code> 默认成员。
我不确定你所说的“加载到内存中”是什么意思。然而,在对象内,编译器不允许重新排列变量。例如:
在
中,变量
对象。当创建c
必须位于b
之后,b
必须位于a
之后>FooFoo
时,它们也按照类声明中显示的顺序构造(初始化),并在销毁Foo
时以相反的顺序析构。由于继承和访问修饰符,它实际上比这更复杂,但这是基本思想。
函数不是数据,因此对齐不是它们所关心的问题。在某些可执行文件格式和/或体系结构中,函数二进制代码实际上占据与数据变量不同的区域,但 C++ 语言对此事实不可知。
内存对齐几乎是编译器自动为您处理的事情。它更多的是一个实现细节。我说“几乎自动”,因为在某些情况下它可能很重要(序列化、ABI 等),但在应用程序中它不应该是一个问题。
关于读取文件(因为您提到了文件头),听起来您正在将文件直接读取到
struct
占用的内存中。我不推荐这种方法,因为填充和对齐问题可能会使您的代码在一个平台上运行,而不是在另一个平台上运行。相反,您应该从文件中一次读取几个原始字节,并通过简单的赋值将它们分配到struct
中。The memory placement/alignment of objects is not contingent on whether its type was declared as a
class
or astruct
. The only difference between aclass
and astruct
in C++ is that a class haveprivate
members by default while a struct havepublic
members by default.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:
The variables
c
must be located afterb
andb
must be located aftera
within aFoo
object. They are also constructed (initialized) in the order shown in the class declaration when aFoo
is created, and destructed in the reverse order when aFoo
is destroyed.It's actually more complicated than this due to inheritance and access modifiers, but that is the basic idea.
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.
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 thestruct
s with simple assignment.是的。从技术上讲,类和结构之间没有区别。唯一的区别是默认的成员访问规范,否则它们是相同的。
是的。
不会。它们不会影响对齐。方法是单独编译的。该对象不包含任何对方法的引用(对于那些说虚拟表确实影响成员的人来说,答案是是和否,但这是一个实现细节,不会影响成员之间的相对差异。允许编译器添加特定于实现的数据到对象)。
好的。不确定这会如何影响任何事情。
为同一事物构造类/结构的不同名称。
选择什么方法?
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.
Yes.
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).
OK. Not sure how that affects anything.
Class/Structs different name for the same thing.
Choose what approach?
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.
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.