C++ 类层次结构中的"对齐"是什么意思?

What does `align` mean in c++ class hierarchy?

本文关键字:quot 是什么 意思 对齐 C++ 层次结构      更新时间:2023-10-16
class Student{
public:
int id;
int age;
};
int main() {
return 0;
}

如果我们使用 gcc 和-fdump-class-hierarchy选项编译这样的程序,gcc 将生成如下所示的.class文件:

Class Student
size=8 align=4
base size=8 base align=4
Student (0x0x7fbafc4a05a0) 0

编译命令:

g++ file.cpp -o file -fdump-class-hierarchy

显然size班级规模,但是alignbase alignbase size在这个文件中是什么意思?

对齐方式与计算机在内存中存储其的位置有关,例如整数,您的学生班级有两个。由于 int 的长度通常为 4 个字节,因此当 int 的表示形式以 4 的倍数对齐时,加载和存储的效率可能会高得多。