c++ - 结构成员对齐和填充明确的活动
问题描述
尝试以下代码- https://onlinegdb.com/H1Sf_PAiI
#include<iostream>
using namespace std;
struct Str
{
char c;
int ch1:56;
double d;
int s;
int ch:32;
}st;
int main()
{
cout<<sizeof st<<endl;
return 0;
}
输出
main.cpp:7:10: warning: width of ‘Str::ch1’ exceeds its type
32
然后我将 ch1 位字段减少到 32 位。有效..!
尝试以下代码- https://onlinegdb.com/BkD4YP0oI
#include<iostream>
using namespace std;
struct Str
{
char c;
int ch1:32;
double d;
int s;
int ch:32;
}st;
int main()
{
cout<<sizeof st<<endl;
return 0;
}
输出
24
上图中的规则 1 和 2 不正确?
解决方案
推荐阅读
- javascript - html-docx-js 在创建 docx 文件时不能应用外部 css 类样式
- python - 为 Teradata DB 表格式化 Python 时间戳
- azure-sql-data-warehouse - ALTER TABLE SWITCH 语句失败。表 X 中的分区 1 定义的范围不是表 Y 中的分区 2 定义的范围的子集
- python - 向 Scrapy Spider 添加标题
- python - 倒计时在 Tkinter 中运行很晚
- matlab - 有没有更快的方法来计算 Matlab 中矩阵的元素指数?
- c++ - 指针或引用的 const 位置
- .htaccess - .htaccess 在子目录中拒绝除 index.php 之外的子目录中的所有内容
- weka - Weka 无法在 MacOS 上打开
- python - 从 Understat.com 抓取特定元素