当前位置:网站首页>如何用程序确认当前系统的存储模式?
如何用程序确认当前系统的存储模式?
2022-07-06 14:40:00 【是北豼不太皮吖】
union 关键字
union 关键字的用法与 struct 的用法非常类似。
union 维护足够的空间来置放多个数据成员中的“一种”,而不是为每一个数据成员配置空间,在 union 中所有的数据成员共用一个空间,同一时间只能储存其中一个数据成员,所有的数据成员具有相同的起始地址。
union StateMachine
{
char character;
int number;
char *str;
double exp;
};
一个 union 只配置一个足够大的空间以来容纳最大长度的数据成员,以上例而言,最大长度是 double 型态,所以 StateMachine 的空间大小就是 double 数据类型的大小。
大小端模式对 union 类型数据的影响
union
{
int i;
char a[2];
}*p, u;
p = &u;
p->a[0] = 0x39;
p->a[1] = 0x38;
p.i 的值应该为多少呢?
其值由系统所采用的 存储模式 和 int 类型 所占存储空间的字节数决定
当int为4字节时
答:当为大端模式时0x39380000
答:当为小端模式时0x00003839
这里需要考虑存储模式:大端模式和小端模式。
大端模式(Big_endian):字数据的高字节存储在低地址中,而字数据的低字节则存放在高地址中。
小端模式(Little_endian):字数据的高字节存储在高地址中,而字数据的低字节则存放在低地址中。
union 型数据所占的空间等于其最大的成员所占的空间。对 union 型的成员的存取都是相对于该联合体基地址的偏移量为 0 处开始,也就是联合体的访问不论对哪个变量的存取都是从 union 的首地址位置开始。
如何用程序确认当前系统的存储模式?
请写一个 C 函数,若处理器是Big_endian 的,则返回 0;若是 Little_endian 的,则返回 1。
假设 int 类型变量 i 被初始化为 1。
以大端模式存储,其内存布局如下图:
以小端模式存储,其内存布局如下图:
变量 i 占 4 个字节,但只有一个字节的值为 1,另外三个字节的值都为 0。如果取出低地址上的值为 0,毫无疑问,这是大端模式;如果取出低地址上的值为 1,毫无疑问,这是小端模式。
可以利用 union 类型数据的特点:所有成员的起始地址一致。
int checkSystem( )
{
union check
{
int i;
char ch;
} c;
c.i = 1;
return (c.ch ==1);
}
可以用这个函数来测试你当前系统的存储模式了。
边栏推荐
- Mise en place d'un environnement de développement OP - tee basé sur qemuv8
- i.mx6ull搭建boa服务器详解及其中遇到的一些问题
- 将MySQL的表数据纯净方式导出
- 中国1,4-环己烷二甲醇(CHDM)行业调研与投资决策报告(2022版)
- Memorabilia of domestic database in June 2022 - ink Sky Wheel
- Daily question 1: force deduction: 225: realize stack with queue
- 【10点公开课】:视频质量评价基础与实践
- qt quick项目offscreen模式下崩溃的问题处理
- Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
- Kohana database
猜你喜欢
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
第3章:类的加载过程(类的生命周期)详解
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
手写ABA遇到的坑
labelimg的安装与使用
[线性代数] 1.3 n阶行列式
Attack and defense world miscall
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
Installation and use of labelimg
The nearest common ancestor of binary (search) tree ●●
随机推荐
Save and retrieve strings
Spatial domain and frequency domain image compression of images
C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
Mongodb (III) - CRUD
Insert sort and Hill sort
CCNA-思科网络 EIGRP协议
Research and investment strategy report of China's VOCs catalyst industry (2022 Edition)
Set status bar style demo
HDU 2008 digital statistics
Four data streams of grpc
ZABBIX proxy server and ZABBIX SNMP monitoring
GPS從入門到放弃(十三)、接收機自主完好性監測(RAIM)
MariaDB database management system learning (I) installation diagram
[10:00 public class]: basis and practice of video quality evaluation
The SQL response is slow. What are your troubleshooting ideas?
Management background --4, delete classification
HDR image reconstruction from a single exposure using deep CNNs阅读札记
Solve project cross domain problems
网络基础入门理解
i.mx6ull搭建boa服务器详解及其中遇到的一些问题