当前位置:网站首页>如何用程序确认当前系统的存储模式?
如何用程序确认当前系统的存储模式?
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);
}
可以用这个函数来测试你当前系统的存储模式了。
边栏推荐
- Force buckle 575 Divide candy
- 二分图判定
- Crawler obtains real estate data
- Anaconda installs third-party packages
- What are the interface tests? What are the general test points?
- Oracle control file and log file management
- i. Mx6ull build boa server details and some of the problems encountered
- 2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
- Four data streams of grpc
- 中国VOCs催化剂行业研究与投资战略报告(2022版)
猜你喜欢

Daily question 1: force deduction: 225: realize stack with queue

LeetCode刷题(十一)——顺序刷题51至55

BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用

2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks

Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题

Attack and defense world ditf Misc

【10点公开课】:视频质量评价基础与实践

Management background --3, modify classification

2500个常用中文字符 + 130常用中英文字符
![[线性代数] 1.3 n阶行列式](/img/6e/54f3a994fc4c2c10c1036bee6715e8.gif)
[线性代数] 1.3 n阶行列式
随机推荐
LeetCode 练习——剑指 Offer 26. 树的子结构
VIP case introduction and in-depth analysis of brokerage XX system node exceptions
Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others
Maximum product of three numbers in question 628 of Li Kou
Research and investment strategy report of China's VOCs catalyst industry (2022 Edition)
Oracle Performance Analysis 3: introduction to tkprof
Mise en place d'un environnement de développement OP - tee basé sur qemuv8
自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
小程序系统更新提示,并强制小程序重启并使用新版本
0 basic learning C language - digital tube
2500个常用中文字符 + 130常用中英文字符
中国固态氧化物燃料电池技术进展与发展前景报告(2022版)
labelimg的安装与使用
i. Mx6ull build boa server details and some of the problems encountered
十二、启动流程
【10点公开课】:视频质量评价基础与实践
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
二分图判定
做接口测试都测什么?有哪些通用测试点?
Build op-tee development environment based on qemuv8