C++结构体内存对齐跨平台测试
测试1,不规则对齐数据。
Code:
#include <stdio.h> #pragma pack(push)
#pragma pack(8)
struct Test8
{
char a;
long long b;
char d;
};
#pragma pack(pop) #pragma pack(push)
#pragma pack(4)
struct Test4
{
char a;
long long b;
char d;
};
#pragma pack(pop) #pragma pack(push)
#pragma pack(1)
struct Test1
{
char a;
long long b;
char d;
};
#pragma pack(pop) struct Test0
{
char a;
long long b;
char d;
}; int main()
{
printf("sizeof(void*)=%d\n", sizeof(void*));
printf("sizeof(long long)=%d\n", sizeof(long long));
printf("sizeof(Test8)=%d\n", sizeof(struct Test8));
printf("sizeof(Tes4)=%d\n", sizeof(struct Test4));
printf( "sizeof(Test1)=%d\n", sizeof(struct Test1));
printf("sizeof(Test0)=%d\n", sizeof(struct Test0)); return ;
}
Output msvc x86:
sizeof(void*)=
sizeof(long long)=
sizeof(Test8)=
sizeof(Tes4)=
sizeof(Test1)=
sizeof(Test0)=
Output msvc x64:
sizeof(void*)=
sizeof(long long)=
sizeof(Test8)=
sizeof(Tes4)=
sizeof(Test1)=
sizeof(Test0)=
Output xcode IOS armV7:
sizeof(void*)=
sizeof(long long)=
sizeof(Test8)=
sizeof(Tes4)=
sizeof(Test1)=
sizeof(Test0)=
Output xcode IOS arm64:
sizeof(void*)=
sizeof(long long)=
sizeof(Test8)=
sizeof(Tes4)=
sizeof(Test1)=
sizeof(Test0)=
Android armv7 simulator output code:
__android_log_print(ANDROID_LOG_DEBUG, "JNITag", "sizeof(void*)=%d\n", sizeof(void*));
__android_log_print(ANDROID_LOG_DEBUG, "JNITag", "sizeof(long long)=%d\n", sizeof(long long));
__android_log_print(ANDROID_LOG_DEBUG, "JNITag", "sizeof(Test8)=%d\n", sizeof(Test8));
__android_log_print(ANDROID_LOG_DEBUG, "JNITag", "sizeof(Tes4)=%d\n", sizeof(Test4));
__android_log_print(ANDROID_LOG_DEBUG, "JNITag", "sizeof(Test1)=%d\n", sizeof(Test1));
__android_log_print(ANDROID_LOG_DEBUG, "JNITag", "sizeof(Test0)=%d\n", sizeof(Test0));
Android armv7 simulator Output:
D/JNITag: sizeof(void*)=
D/JNITag: sizeof(long long)=
D/JNITag: sizeof(Test8)=
D/JNITag: sizeof(Tes4)=
D/JNITag: sizeof(Test1)=
D/JNITag: sizeof(Test0)=
测试二,简单数组测试,源码:
#include <stdio.h> #pragma pack(push)
#pragma pack(8)
struct Test8
{
char a[];
};
#pragma pack(pop) #pragma pack(push)
#pragma pack(4)
struct Test4
{
char a[];
};
#pragma pack(pop) #pragma pack(push)
#pragma pack(1)
struct Test1
{
char a[];
};
#pragma pack(pop) struct Test0
{
char a[];
}; int main()
{
printf("sizeof(void*)=%d\n", sizeof(void*));
printf("sizeof(long long)=%d\n", sizeof(long long));
printf("sizeof(Test8)=%d\n", sizeof(struct Test8));
printf("sizeof(Tes4)=%d\n", sizeof(struct Test4));
printf( "sizeof(Test1)=%d\n", sizeof(struct Test1));
printf("sizeof(Test0)=%d\n", sizeof(struct Test0)); return ;
}
msvc x86:
sizeof(void*)=
sizeof(long long)=
sizeof(Test8)=
sizeof(Tes4)=
sizeof(Test1)=
sizeof(Test0)=
msvc x64:
sizeof(void*)=
sizeof(long long)=
sizeof(Test8)=
sizeof(Tes4)=
sizeof(Test1)=
sizeof(Test1)=
iphone armv7:
sizeof(void*)=
sizeof(long long)=
sizeof(Test8)=
sizeof(Tes4)=
sizeof(Test1)=
sizeof(Test0)=
iphone arm64:
sizeof(void*)=
sizeof(long long)=
sizeof(Test8)=
sizeof(Tes4)=
sizeof(Test1)=
sizeof(Test0)=
android armv7 simulator:
D/JNITag: sizeof(void*)=
D/JNITag: sizeof(long long)=
D/JNITag: sizeof(Test8)=
D/JNITag: sizeof(Tes4)=
D/JNITag: sizeof(Test1)=
D/JNITag: sizeof(Test0)=
测试三,当结构体包含pack1的结构体时
code:
#include <stdio.h>
#include <stddef.h> #pragma pack(push)
#pragma pack(1)
struct Test1
{
char a[];
long long b;
char d[];
int e;
};
#pragma pack(pop) #pragma pack(push)
#pragma pack(8)
struct Owner8
{
char c1;
struct Test1 a;
int i2;
}; #pragma pack(pop) #pragma pack(push)
#pragma pack(4)
struct Owner4
{
char c1;
struct Test1 a;
int i2;
}; #pragma pack(pop) #pragma pack(push)
#pragma pack(1)
struct Owner1
{
char c1;
struct Test1 a;
int i2;
}; #pragma pack(pop) struct Owner0
{
char c1;
struct Test1 a;
int i2;
}; int main()
{
printf("sizeof(void*)=%d\n", sizeof(void*));
printf("sizeof(long long)=%d\n", sizeof(long long));
printf( "sizeof(Test1)=%d\n", sizeof(struct Test1)); printf("---------------------------------------\n");
printf("sizeof(Owner8)=%d\n", sizeof(struct Owner8));
printf("offsetof(Owner8, a)=%d\n", offsetof( struct Owner8, a));
printf("offsetof(Owner8, i2)=%d\n", offsetof( struct Owner8, i2)); printf("---------------------------------------\n");
printf("sizeof(Owner4)=%d\n", sizeof(struct Owner4));
printf("offsetof(Owner4, a)=%d\n", offsetof( struct Owner4, a));
printf("offsetof(Owner4, i2)=%d\n", offsetof( struct Owner4, i2)); printf("---------------------------------------\n");
printf("sizeof(Owner1)=%d\n", sizeof(struct Owner1));
printf("offsetof(Owner1, a)=%d\n", offsetof( struct Owner1, a));
printf("offsetof(Owner1, i2)=%d\n", offsetof( struct Owner1, i2)); printf("---------------------------------------\n");
printf("sizeof(Owner0)=%d\n", sizeof(struct Owner0));
printf("offsetof(Owner0, a)=%d\n", offsetof( struct Owner0, a));
printf("offsetof(Owner0, i2)=%d\n", offsetof( struct Owner0, i2)); return ;
}
msvc x86:
sizeof(void*)=
sizeof(long long)=
sizeof(Test1)=
---------------------------------------
sizeof(Owner8)=
offsetof(Owner8, a)=
offsetof(Owner8, i2)=
---------------------------------------
sizeof(Owner4)=
offsetof(Owner4, a)=
offsetof(Owner4, i2)=
---------------------------------------
sizeof(Owner1)=
offsetof(Owner1, a)=
offsetof(Owner1, i2)=
---------------------------------------
sizeof(Owner0)=
offsetof(Owner0, a)=
offsetof(Owner0, i2)=
msvc x64:
sizeof(void*)=
sizeof(long long)=
sizeof(Test1)=
---------------------------------------
sizeof(Owner8)=
offsetof(Owner8, a)=
offsetof(Owner8, i2)=
---------------------------------------
sizeof(Owner4)=
offsetof(Owner4, a)=
offsetof(Owner4, i2)=
---------------------------------------
sizeof(Owner1)=
offsetof(Owner1, a)=
offsetof(Owner1, i2)=
---------------------------------------
sizeof(Owner0)=
offsetof(Owner0, a)=
offsetof(Owner0, i2)=
xcode ios armv7:
sizeof(void*)=
sizeof(long long)=
sizeof(Test1)=
---------------------------------------
sizeof(Owner8)=
offsetof(Owner8, a)=
offsetof(Owner8, i2)=
---------------------------------------
sizeof(Owner4)=
offsetof(Owner4, a)=
offsetof(Owner4, i2)=
---------------------------------------
sizeof(Owner1)=
offsetof(Owner1, a)=
offsetof(Owner1, i2)=
---------------------------------------
sizeof(Owner0)=
offsetof(Owner0, a)=
offsetof(Owner0, i2)=
xcode ios arm64:
sizeof(void*)=
sizeof(long long)=
sizeof(Test1)=
---------------------------------------
sizeof(Owner8)=
offsetof(Owner8, a)=
offsetof(Owner8, i2)=
---------------------------------------
sizeof(Owner4)=
offsetof(Owner4, a)=
offsetof(Owner4, i2)=
---------------------------------------
sizeof(Owner1)=
offsetof(Owner1, a)=
offsetof(Owner1, i2)=
---------------------------------------
sizeof(Owner0)=
offsetof(Owner0, a)=
offsetof(Owner0, i2)=
android armv7 simulator:
D/JNITag: sizeof(void*)=
D/JNITag: sizeof(long long)=
D/JNITag: sizeof(Test1)=
D/JNITag: ---------------------------------------
D/JNITag: sizeof(Owner8)=
D/JNITag: offsetof(Owner8, a)=
D/JNITag: offsetof(Owner8, i2)=
D/JNITag: ---------------------------------------
D/JNITag: sizeof(Owner4)=
D/JNITag: offsetof(Owner4, a)=
D/JNITag: offsetof(Owner4, i2)=
D/JNITag: ---------------------------------------
D/JNITag: sizeof(Owner1)=
D/JNITag: offsetof(Owner1, a)=
D/JNITag: offsetof(Owner1, i2)=
D/JNITag: ---------------------------------------
D/JNITag: sizeof(Owner0)=
D/JNITag: offsetof(Owner0, a)=
D/JNITag: offsetof(Owner0, i2)=
C++结构体内存对齐跨平台测试的更多相关文章
- C++ struct结构体内存对齐
•小试牛刀 我们自定义两个结构体 A 和 B: struct A { char c1; char c2; int i; double d; }; struct B { char c1; int i; ...
- 【APUE】Chapter17 Advanced IPC & sign extension & 结构体内存对齐
17.1 Introduction 这一章主要讲了UNIX Domain Sockets这样的进程间通讯方式,并列举了具体的几个例子. 17.2 UNIX Domain Sockets 这是一种特殊s ...
- 关于结构体内存对齐方式的总结(#pragma pack()和alignas())
最近闲来无事,翻阅msdn,在预编译指令中,翻阅到#pragma pack这个预处理指令,这个预处理指令为结构体内存对齐指令,偶然发现还有另外的内存对齐指令aligns(C++11),__declsp ...
- [C/C++] 结构体内存对齐用法
一.为什么要内存对齐 经过内存对齐之后,CPU的内存访问速度大大提升; 内存空间按照byte划分,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定变量的时候经常在特定的内 ...
- [C/C++] 结构体内存对齐:alignas alignof pack
简述: alignas(x):指定结构体内某个成员的对齐字节数,指定的对齐字节数不能小于它原本的字节数,且为2^n; #pragma pack(x):指定结构体的对齐方式,只能缩小结构体的对齐数,且为 ...
- go语言结构体内存对齐
cpu要想从内存读取数据,需要通过地址总线,把地址传输给内存,内存准备好数据,输出到数据总线,交给cpu,如果地址总线只有8根,那这个地址就只有8位可以表示[0,255]256个地址,因为表示不了更多 ...
- C语言-结构体内存对齐
C语言结构体对齐也是老生常谈的话题了.基本上是面试题的必考题.内容虽然很基础,但一不小心就会弄错.写出一个struct,然后sizeof,你会不会经常对结果感到奇怪?sizeof的结果往往都比你声明的 ...
- c 结构体内存对齐详解
0x00简介 首先要知道结构体的对齐规制 1.第一个成员在结构体变量偏移量为0的地址处 2.其他成员变量对齐到某个数字的整数倍的地址处 对齐数=编辑器默认的一个对齐数与该成员大小的较小值 vs中默认的 ...
- C语言中结构体内存对齐
先写一个小程序: #include<stdio.h> struct student { int a; char k; short m; }; int main() { st ...
随机推荐
- Apache Shiro 学习记录5
本来这篇文章是想写从Factory加载ini配置到生成securityManager的过程的....但是貌似涉及的东西有点多...我学的又比较慢...很多类都来不及研究,我又怕等我后面的研究了前面的都 ...
- 【错误总结】java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor
运行环境 JDK1.7 64位 Window 7 64位 Ecplise Java EE IDE Struts 2.3.9 Tomcat 7 说明: ...
- 2 column数据构成主键的表转化为1 column为主键的表
问题:假设有张学生成绩表(tb)如下:姓名 课程 分数张三 语文 74张三 数学 83张三 物理 93张三 德语 null李四 语文 74李四 数学 84李四 物理 94李四 英语 80想变成(得到如 ...
- ASP.NET、JAVA跨服务器远程上传文件(图片)的相关解决方案整合
一.图片提交例: A端--提交图片 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string u ...
- python --enable-shared
https://github.com/docker-library/python/issues/21 例如编译安装python3.5.2,脚本如下: wget https://s3.cn-north- ...
- ASP.NET MVC RenderPartial和Partial的区别
背景:ASP.NET MVC 4.0 @{ Html.RenderPartial(...); } public static void RenderPartial(this HtmlHelper ht ...
- 总结初用erlang 时的遇到一些问题
算起来接触erlang 三个多月快四个月来,期间从零开始看书写erlang代码.修改RabbitMQ.业务开发.系统调优,总算是有点入门了. 最难受的是边学边修改RabbitMQ,开始真心的看不懂,不 ...
- jquery判断多个input输入框不能输入相同的值
function checkName(data){ //校验联系信息不能一致 var values=""; $(".t").each(function(i,it ...
- iOS 编译时的警告导致无法通过编译
今天编译react native的代码,发现了2个警告,但是系统却当做错误,不能编译成功,查看了一下编译选项,看到了如下配置: 注意到这个-Werror 了吗? 就是这个标志导致系统把所有的 警告都当 ...
- 不得不说的wepapi 优化
1:在你的ASP.NET Web API中使用GZIP 或 Deflate .对于减少响应包的大小和响应速度,压缩是一种简单而有效的方式.这是一个非常有必要使用的功能,你可以查看更多关于压缩的文章在我 ...