结构中的内存布局差异 [英] Memory layout differences in structs
问题描述
我在C ++中具有以下结构
I have the following structure in C++
struct A {
int a;
double b;
float c;
}
此结构与添加了功能的结构之间的内存布局是否有所不同?
Is there a difference in memory layout between this struct and one with a function added to it?
struct B {
int a;
double b;
float c;
void foo();
}
B::foo() { //do stuff }
推荐答案
C ++标准保证,只要C ++类/struct符合成为POD的标准(普通旧数据").那么POD是什么意思?
The C++ standard guarantees that memory layouts of a C struct and a C++ class (or struct -- same thing) will be identical, provided that the C++ class/struct fits the criteria of being POD ("Plain Old Data"). So what does POD mean?
如果满足以下条件,则类或结构为POD:
A class or struct is POD if:
所有数据成员都是公共的,它们本身是POD或基本类型(但不是引用类型或指向成员的指针类型),或此类的数组
All data members are public and themselves POD or fundamental types (but not reference or pointer-to-member types), or arrays of such
- 它没有用户定义的构造函数,赋值运算符或析构函数
- 它没有虚拟功能
- 它没有基类
是的,在您的情况下,内存布局是相同的.
So yes in your case, the memory layout is the same.
这篇关于结构中的内存布局差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!