当前位置:网站首页>如何用程序确认当前系统的存储模式?
如何用程序确认当前系统的存储模式?
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);
}
可以用这个函数来测试你当前系统的存储模式了。
边栏推荐
- Oracle-控制文件及日志文件的管理
- Classic sql50 questions
- Force buckle 575 Divide candy
- Xiaoman network model & http1-http2 & browser cache
- 基于 QEMUv8 搭建 OP-TEE 开发环境
- GPS from getting started to giving up (XX), antenna offset
- GPS from entry to abandonment (XIV), ionospheric delay
- GPS from getting started to giving up (16), satellite clock error and satellite ephemeris error
- Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
- CCNA-思科网络 EIGRP协议
猜你喜欢

CCNA-思科网络 EIGRP协议

BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
![[sciter]: encapsulate the notification bar component based on sciter](/img/08/a3dd409261054052291e99dd28af11.png)
[sciter]: encapsulate the notification bar component based on sciter

墨西哥一架飞往美国的客机起飞后遭雷击 随后安全返航

自制J-Flash烧录工具——Qt调用jlinkARM.dll方式

Common sense: what is "preservation" in insurance?

3DMax指定面贴图
The SQL response is slow. What are your troubleshooting ideas?

Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing

2500 common Chinese characters + 130 common Chinese and English characters
随机推荐
GPS from getting started to giving up (19), precise ephemeris (SP3 format)
Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
GPS from getting started to giving up (XIII), receiver autonomous integrity monitoring (RAIM)
将MySQL的表数据纯净方式导出
Learn the principle of database kernel from Oracle log parsing
Wechat red envelope cover applet source code - background independent version - source code with evaluation points function
每日一题:力扣:225:用队列实现栈
中国固态氧化物燃料电池技术进展与发展前景报告(2022版)
Web APIs DOM 时间对象
GPS from entry to abandonment (XVII), tropospheric delay
C#實現水晶報錶綁定數據並實現打印4-條形碼
HDR image reconstruction from a single exposure using deep CNNs阅读札记
GPS from getting started to giving up (12), Doppler constant speed
[10:00 public class]: basis and practice of video quality evaluation
Chapter 3: detailed explanation of class loading process (class life cycle)
Set status bar style demo
新手程序员该不该背代码?
墨西哥一架飞往美国的客机起飞后遭雷击 随后安全返航
GD32F4XX串口接收中断和闲时中断配置
Xiaoman network model & http1-http2 & browser cache