当前位置:网站首页>union中有struct的情况-从内存分析
union中有struct的情况-从内存分析
2022-07-30 05:39:00 【Linke66】
union中的每个成员共用一块内存,且首地址相同;
这块内存必须大于等于union中最大成员的字节数。
举个例子:
union U中有两个成员,
一个是int型的a,4个字节;
一个是结构体STU,考虑对齐,16个字节。
struct STU {
int b;
long long c;
};
union U{
int a;
STU stu;
};
首先使用结构体初始化union U,查看其内存分布情况:
U u;
STU m_stu;
m_stu.b = 6;
m_stu.c = 66;
u.stu = m_stu;
cout << "struct 中 (int)b=" << u.stu.b << " (long)c=" << u.stu.c << endl;
内存分布情况如下图,union的初始地址是0x007CF9E8,分配了16个字节的内存;
前面4个字节存放成员(int)b,后面四个字节是对齐的4个字节;
再往后面8个字节的内存存放(long long)c;
所以b=6;c=66;
下面用union另一个成员(int)a来初始化union U;
u.a = INT_MAX;
cout << "(int)a=" << u.a << endl;
cout << "struct 中 (int)b=" << u.stu.b << " (long)c=" << u.stu.c << endl;
内存中的情况如下图
所以输出为:
下面做一个测试,考虑以下代码
现在union中两个成员,结构体STU类型的stu和int型的a都为4个字节;
我们先初始化stu的四个成员为字符’a’,那么查看stu的四个字符成员,自然都是’a’;
接下来我们将使用union U中的a成员,初始化为1111638594,即16进制的 42 42 42 42,而字符’B’的ascii码为42(16进制),那么这个时候假如我们去输出结构体的4个成员,它会是什么呢?结果在下面。
struct STU {
char ch1;
char ch2;
char ch3;
char ch4;
};
union U{
int a;
STU stu;
};
int main() {
U u;
STU m_stu;
m_stu.ch1 = 'a'; //ascii为97,即16进制61
m_stu.ch2 = 'a';
m_stu.ch3 = 'a';
m_stu.ch4 = 'a';
u.stu = m_stu;
cout << "struct 中 (char)ch1=" << u.stu.ch1
<< " (char)ch2=" << u.stu.ch2
<< " (char)ch3=" << u.stu.ch3
<< " (char)ch4=" << u.stu.ch4
<< endl;
u.a = 1111638594;//16进制为 42 42 42 42 ,ascii为42对应的字符是B
cout << "(int)a=" << u.a << endl;
cout << "struct 中 (char)ch1=" << u.stu.ch1
<< " (char)ch2=" << u.stu.ch2
<< " (char)ch3=" << u.stu.ch3
<< " (char)ch4=" << u.stu.ch4
<< endl;
}
根据上面的分析,我们不难分析出,这时候输出结构体的4个成员,都是字符‘B’
边栏推荐
- MySQL的 DDL和DML和DQL的基本语法
- Ranking of grades (Huazhong University of Science and Technology postgraduate examination questions) (DAY 87)
- St. Regis Takeaway Project: New dishes and dishes paged query
- asyncawait和promise的区别
- HCIP-第九天-BGP(边界网关协议)
- mysql time field is set to current time by default
- 手把手教你设计一个CSDN系统
- 字符串(一) 哈希
- torch.optim.Adam()
- 646.最长数对链(动态规划)
猜你喜欢

453.最小操作数使数组元素相等

Teach you to completely uninstall MySQL

idea 编译protobuf 文件的设置使用

This dependency was not found:

应用实践 | Apache Doris 在百度智能云计费账单系统的应用实践

Error: npm ERR code EPERM

手把手教你设计一个CSDN系统

【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码

MySQL fuzzy query performance optimization

Personal blog system (with source code)
随机推荐
[Image detection] Research on cumulative weighted edge detection method based on grayscale image with matlab code
MySQL模糊查询性能优化
create-nuxt-app创建出来的项目没有server
4461. 范围分区(Google Kickstart2022 Round C Problem B)
ClickHouse data insert, update and delete operations SQL
手把手教你彻底卸载MySQL
cookie和session区别
665.非递减数列
[Mysql] DATEDIFF function
MySQL 数据库基础知识(系统化一篇入门)
Teach you to completely uninstall MySQL
My first understanding of MySql, and the basic syntax of DDL and DML and DQL in sql statements
海量号码需要保存,如何才能尽可能少地占用内存?
Solve phpstudy unable to start MySQL service
The difference between asyncawait and promise
Ranking of grades (Huazhong University of Science and Technology postgraduate examination questions) (DAY 87)
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
应用实践 | Apache Doris 在百度智能云计费账单系统的应用实践
Different lower_case_table_names settings for server ('1') and data dictionary ('0') solution