当前位置:网站首页>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 .
边栏推荐
- 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
- Daily question 1: force deduction: 225: realize stack with queue
- Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
- Web APIs DOM 时间对象
- Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode
- 二叉(搜索)树的最近公共祖先 ●●
- Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
- Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
- ThreadLocal详解
- Should novice programmers memorize code?
猜你喜欢
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
pytorch_YOLOX剪枝【附代码】
树的先序中序后序遍历
Daily question 1: force deduction: 225: realize stack with queue
ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
关于声子和热输运计算中BORN电荷和non-analytic修正的问题
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
自定义 swap 函数
Build op-tee development environment based on qemuv8
随机推荐
pytorch_ Yolox pruning [with code]
Leetcode exercise - Sword finger offer 26 Substructure of tree
Notes de développement du matériel (10): flux de base du développement du matériel, fabrication d'un module USB à RS232 (9): création de la Bibliothèque d'emballage ch340g / max232 SOP - 16 et Associa
Attack and defense world miscall
uniapp滑动到一定的高度后固定某个元素到顶部效果demo(整理)
树的先序中序后序遍历
MySQL约束的分类、作用及用法
NetXpert XG2帮您解决“布线安装与维护”难题
PVL EDI 项目案例
(18) LCD1602 experiment
SQL server generates auto increment sequence number
Advantages of link local address in IPv6
Build op-tee development environment based on qemuv8
【编译原理】做了一半的LR(0)分析器
Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
go多样化定时任务通用实现与封装
What are the specific steps and schedule of IELTS speaking?
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution