当前位置:网站首页>如何用程序确认当前系统的存储模式?
如何用程序确认当前系统的存储模式?
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);
}
可以用这个函数来测试你当前系统的存储模式了。
边栏推荐
- 图像的spatial domain 和 frequency domain 图像压缩
- MySQL约束的分类、作用及用法
- Mise en place d'un environnement de développement OP - tee basé sur qemuv8
- Kohana database
- 2022年6月国产数据库大事记-墨天轮
- [sciter]: encapsulate the notification bar component based on sciter
- Oracle-控制文件及日志文件的管理
- UNI-Admin基础框架怎么关闭创建超级管理员入口?
- 3DMax指定面贴图
- Research and investment strategy report of China's VOCs catalyst industry (2022 Edition)
猜你喜欢
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
Adjustable DC power supply based on LM317
GPS from entry to abandonment (XVII), tropospheric delay
(十八)LCD1602实验
GPS from getting started to giving up (12), Doppler constant speed
Web APIs DOM 时间对象
GPS from entry to abandonment (XIV), ionospheric delay
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
C#实现水晶报表绑定数据并实现打印4-条形码
随机推荐
Oracle control file and log file management
Daily question 1: force deduction: 225: realize stack with queue
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
Four data streams of grpc
MySQL约束的分类、作用及用法
Data storage (1)
GPS從入門到放弃(十三)、接收機自主完好性監測(RAIM)
2021 geometry deep learning master Michael Bronstein long article analysis
数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
CCNA Cisco network EIGRP protocol
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
插入排序与希尔排序
Attack and defense world miscall
[linear algebra] determinant of order 1.3 n
Spatial domain and frequency domain image compression of images
Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
C # realizes crystal report binding data and printing 4-bar code
(十八)LCD1602实验
VIP case introduction and in-depth analysis of brokerage XX system node exceptions
Build op-tee development environment based on qemuv8