关于struct的空间问题
  • 板块学术版
  • 楼主ppip嘟嘟嘟
  • 当前回复3
  • 已保存回复3
  • 发布时间2023/2/21 19:24
  • 上次更新2023/10/24 00:10:20
查看原帖
关于struct的空间问题
374433
ppip嘟嘟嘟楼主2023/2/21 19:24
#include <bits/stdc++.h>
using namespace std;
struct i2___ {int x,y;}; // 名字太短制表符会崩
struct i1_ll1 {int x;long long y;};
struct i3_ll1 {int x,y,z;long long w;};
struct s3_i1 {short x,y,z;int w;};
struct s1_i1_ll1 {short x;int y; long long z;};
template <typename T>
void test(int expected) {
	cout<<"Testsing: "<<typeid(T).name()+1<<",\texpected=\t"<<expected<<",\tsize=\t"<<sizeof(T)<<endl;
}
int main() {
	test<i2___>(8);
	test<i1_ll1>(12);
	test<i3_ll1>(20);
	test<s3_i1>(10);
	test<s1_i1_ll1>(14);
	return 0;
}
Testsing: i2___,        expected=       8,      size=   8
Testsing: i1_ll1,       expected=       12,     size=   16
Testsing: i3_ll1,       expected=       20,     size=   24
Testsing: s3_i1,        expected=       10,     size=   12
Testsing: s1_i1_ll1,    expected=       14,     size=   16

结论:结构体有莫名其妙的空间常数。无法解释。

2023/2/21 19:24
加载中...