当前位置:网站首页>How to confirm the storage mode of the current system by program?
How to confirm the storage mode of the current system by program?
2022-07-06 22:31:00 【It's Beichen not too PI acridine】
union keyword
union The usage of key words and struct It's very similar to .
union Maintain enough space to place... In multiple data members “ A kind of ”, Instead of configuring space for each data member , stay union All data members in share a space , Only one data member can be stored at a time , All data members have the same starting address .
union StateMachine
{
char character;
int number;
char *str;
double exp;
};
One union Only configure a large enough space to accommodate the maximum length of data members , Take the example above , The maximum length is double Form , therefore StateMachine The size of the space is double The size of the data type .
Large and small mode pairs union The impact of type data
union
{
int i;
char a[2];
}*p, u;
p = &u;
p->a[0] = 0x39;
p->a[1] = 0x38;
p.i What should be the value of ?
Its value is adopted by the system Storage mode and int type The number of bytes in the storage space is determined
When int by 4 Byte time
answer : When in big end mode 0x39380000
answer : When in small end mode 0x00003839
Here we need to consider the storage mode : Big end mode and small end mode .
Big end model (Big_endian): The high bytes of word data are stored in the low address , The low byte of word data is stored in the high address .
The small end model (Little_endian): The high byte of word data is stored in the high address , The low byte of word data is stored in the low address .
union The space occupied by type data is equal to the space occupied by its largest member . Yes union The members of type are accessed relative to the base address of the union. The offset is 0 Start at , That is to say, no matter which variable is accessed by the consortium, it is accessed from union The first address of .
How to confirm the storage mode of the current system with a program ?
Please write one. C function , If the processor is Big_endian Of , Then return to 0; if Little_endian Of , Then return to 1.
hypothesis int Type variable i Is initialized to 1.
Store in big end mode , The memory layout is shown below :
Store in small end mode , The memory layout is shown below :
Variable i Occupy 4 Bytes , But the value of only one byte is 1, The values of the other three bytes are 0. If the value on the low address is 0, without doubt , This is the big end mode ; If the value on the low address is 1, without doubt , This is the small end mode .
You can use union Characteristics of type data : All members have the same starting address .
int checkSystem( )
{
union check
{
int i;
char ch;
} c;
c.i = 1;
return (c.ch ==1);
}
You can use this function to test the storage mode of your current system .
边栏推荐
- [Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
- What are the specific steps and schedule of IELTS speaking?
- Mysql database basic operations DML
- Applet system update prompt, and force the applet to restart and use the new version
- Clip +json parsing converts the sound in the video into text
- 自定义 swap 函数
- 数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
- 2022-07-05 stonedb sub query processing parsing time analysis
- Data storage (1)
- npm无法安装sharp
猜你喜欢
NPDP认证|产品经理如何跨职能/跨团队沟通?
0 basic learning C language - interrupt
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
[线性代数] 1.3 n阶行列式
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
机试刷题1
Advantages of link local address in IPv6
在IPv6中 链路本地地址的优势
Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
随机推荐
自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
做国外LEAD2022年下半年几点建议
CCNA Cisco network EIGRP protocol
手写ABA遇到的坑
poj 1094 Sorting It All Out (拓扑排序)
Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
做接口测试都测什么?有哪些通用测试点?
Learn the principle of database kernel from Oracle log parsing
2022-07-05 use TPCC to conduct sub query test on stonedb
柔性数组到底如何使用呢?
UVa 11732 – strcmp() Anyone?
pytorch_YOLOX剪枝【附代码】
每日一题:力扣:225:用队列实现栈
Inno setup packaging and signing Guide
Lora sync word settings
Management background --5, sub classification
Memorabilia of domestic database in June 2022 - ink Sky Wheel
2022-07-05 使用tpcc对stonedb进行子查询测试
SQL server generates auto increment sequence number