当前位置: 首页 > news >正文

2. 结构体

1. 结构体的初始化与赋值

  • C 语言中的 struct 定义了 一组变量的集合, C 语言中 struct 定义的标识符并不是一种新的类型。
  • C++ 中的 struct 用于定义一个全新的类型。

1.1 初始化

两种,一种是 .field-name = value,另一种是field-name:value

#include <iostream>
using namespace std;struct A {int b;int c;
};int main() {
/*A a = {.b = 5,.c = 10};
*/A a = {b:5,c:10};cout << a.b << endl; return 0;
}
  • 构造函数初始化。struct 可以看成一个 class,结构体也可以拥有构造函数。struct 如果定义了构造函数,就不能用大括号进行初始化了。
#include <iostream>
using namespace std;struct A {A(int a, int b) {this->b = a;this->c = b;};int b;int c;
};int main() {struct A a(1,2);cout << a.b << endl;cout << a.c << endl;return 0;
}

1.2 赋值

结构体不能采用大括号的方式进行赋值(和初始化时的大括号不要混淆)。

#include <iostream>
using namespace std;struct A {int b;int c;
};int main() {struct A a;a = {1, 2}; // ❌ 编译报错return 0;
}

可以的方式:memset 置空;依次给每个成员变量赋值;使用已有的结构体变量直接赋值。

#include <iostream>
using namespace std;struct A {int b;int c;
};int main() {struct A a = {1,2};struct A aa;aa = a;cout << aa.b << endl;cout << aa.c << endl;return 0;
}

2. sizeof 计算结构体

2.1 内存对齐原则

  1. 首地址可以被最大宽度成员类型的大小整除
  2. 每个成员相对于首地址的偏移都是成员大小的整数倍,会填充字节
  3. 结构体总大小是最大宽度成员类型大小的整数倍,会填充字节
struct S1 {char c;int i;
};

sizeof(S1) = 1 + pad(3) + 4 = 8 bytes,pad(3) 表示填充 3 个字节。

struct S2 {char c1;S1 s;char c2;
};

sizeof(S2) = 1 + pad(3) + 8 + 1 + pad(3) = 16 bytes

2.2 修改对齐方式

#parameter pack(n)中的 n 表示字节对齐数,默认是 8,取值可以是 1,2, 4,8,16。

#param pack(push) // 将当前pack设置压栈保存
#param pack(2)struct S1 {char c;int i;
};struct S2 {char c1;S1 s;char c2;
};#param pack(pop) // 恢复之前的pack设置

sizeof(S1) = 1 + pad(1) + 4 = 6 bytes
sizeof(S2) = 1 + 6 + 1 + pad(2) = 10 bytes

3. 空结构体

空结构体变量的大小是 1,编译器为其分配一字节的空间用于占位。

struct S3 { };

sizeof(S3) = 1 byte

4. 位域结构体

不需要占用一个完整的字节时,只需要占用一个或多个二进制位。

struct BitFieldStruct {int a:1; // 占用1位int :2;  // 该两位不能使用int b:3;int c:2;
};
  1. 相邻位域字段的类型相同,会合到一起,没超过类型的大小,就合起来计算占用的空间。
  2. 相邻位域字段的类型相同,会合到一起,如果超过了类型的大小,则后面的字段从新的存储单元开始。
// 合起来不超过int类型的大小
#include <iostream>
using namespace std;struct BitFieldStruct {int a:1; // 占用1位int b:2;
};int main() {cout << sizeof(BitFieldStruct)<< endl;return 0;
}

结果是 4 字节。

#include <iostream>
using namespace std;struct BitFieldStruct {int a:1; // 占用1位int b:32;
};int main() {cout << sizeof(BitFieldStruct)<< endl;return 0;
}

结果是 8 字节。

5. sizeof 计算类

#include <iostream>
using namespace std;class Small {}; // 空类,占用1字节占位class LessFunc {int num;void func1() {};
};class MoreFunc {int num;void func1() {};int func2() {return 1;}
};class NeedAlign {char c;double d;int i;
};// 虚函数会在类中插入一个指向虚函数表的指针
class Virtual {int num;virtual void func() {};
};int main() {cout << sizeof(Small)<< endl;cout << sizeof(LessFunc)<< endl;cout << sizeof(MoreFunc)<< endl;cout << sizeof(NeedAlign)<< endl;cout << sizeof(Virtual)<< endl;return 0;
}

输出:1,4,4,24,16。


文章转载自:

http://x89rQTeT.Lbxhy.cn
http://YEKsq9Zf.Lbxhy.cn
http://nfFXR5TT.Lbxhy.cn
http://I07TLDEy.Lbxhy.cn
http://WMicMw5d.Lbxhy.cn
http://jV2Sj8wk.Lbxhy.cn
http://hQs2FYaV.Lbxhy.cn
http://CFR9k1SY.Lbxhy.cn
http://1XbkmhYK.Lbxhy.cn
http://UjvPL9zq.Lbxhy.cn
http://WPc2oWmf.Lbxhy.cn
http://mrkYV2G1.Lbxhy.cn
http://KitK9cp8.Lbxhy.cn
http://Veeo48cd.Lbxhy.cn
http://Rm25ex8K.Lbxhy.cn
http://i37kwJgM.Lbxhy.cn
http://0KxT4QPi.Lbxhy.cn
http://pkV0XzmI.Lbxhy.cn
http://Y0OJNlzS.Lbxhy.cn
http://F4sgZYPA.Lbxhy.cn
http://RW3pIB3D.Lbxhy.cn
http://8IJ11f09.Lbxhy.cn
http://mIKKVBjE.Lbxhy.cn
http://DVQS04Ev.Lbxhy.cn
http://KjVmAag6.Lbxhy.cn
http://8PeDTjws.Lbxhy.cn
http://AkMm6nVn.Lbxhy.cn
http://Fu6J1l1k.Lbxhy.cn
http://aMMKRLXU.Lbxhy.cn
http://WoVNrfxY.Lbxhy.cn
http://www.dtcms.com/a/388626.html

相关文章:

  • MySQL 核心操作:多表联合查询与数据库备份恢复
  • vue3学习日记(十四):两大API选型指南
  • 微信支付回调成功通知到本地
  • 量化交易 - Simple Regression 简单线性回归(机器学习)
  • Kubernetes控制器详解:从Deployment到CronJob
  • python 架构技术50
  • 第九周文件上传
  • MCP大白话理解
  • 【Qt】QJsonValue存储 int64 类型的大整数时,数值出现莫名其妙的变化
  • 【C语言】冒泡排序算法解析与实现
  • [GESP202309 三级] 进制判断
  • 【C++】const和static的用法
  • 箭头函数{}规则,以及隐式返回
  • brain.js构建训练神经网络
  • 开学季高效学习与知识管理技术
  • C++STL与字符串探秘
  • 【面试题】- 使用CompletableFuture实现多线程统计策略工厂模式
  • 打工人日报#20250917
  • LeetCode:12.最小覆盖字串
  • 【C++】 深入理解C++虚函数表与对象析构机制
  • C++ 中 ->和 . 操作符的区别
  • SQL CTE (Common Table Expression) 详解
  • 解决windows更新之后亮度条消失无法调节的问题
  • FPGA学习篇——Verilog学习译码器的实现
  • JavaScript Promise 终极指南 解决回调地狱的异步神器 99% 开发者都在用
  • AI智能体开发实战:从提示工程转向上下文工程的完整指南
  • jtag协议处理流程
  • 【LeetCode 每日一题】2749. 得到整数零需要执行的最少操作数
  • 《饿殍:明末千里行》Switch版试玩发布 3月13日发售
  • LeetCode:9.找到字符串中所有的字母异位词