查看内存管理是大端还是小端,使用union结构,union结构是里面的各个数据共用一个内存
//大小端查看
#include <iostream>
using namespace std;
union un{
short int s;
char c[sizeof(short int)];
};
int main(){
un test;
test.s=0x0102;
cout<<sizeof(short int)<<endl;
cout<<"c[0]="<<(int)test.c[0]<<endl;
cout<<"c[1]="<<(int)test.c[1]<<endl;
if(test.c[0]==1&&test.c[1]==2)cout<<"little "<<endl;
if(test.c[1]==1&&test.c[0]==2)cout<<"big"<<endl;
}
本文介绍了一种使用C++ union结构来检测系统是大端还是小端的方法。通过实例演示了如何设置短整型变量并在内存中读取其高位与低位字节,以此判断系统的字节序。
2222

被折叠的 条评论
为什么被折叠?



