测试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++结构体内存对齐跨平台测试的更多相关文章

  1. C++ struct结构体内存对齐

    •小试牛刀 我们自定义两个结构体 A 和 B: struct A { char c1; char c2; int i; double d; }; struct B { char c1; int i; ...

  2. 【APUE】Chapter17 Advanced IPC & sign extension & 结构体内存对齐

    17.1 Introduction 这一章主要讲了UNIX Domain Sockets这样的进程间通讯方式,并列举了具体的几个例子. 17.2 UNIX Domain Sockets 这是一种特殊s ...

  3. 关于结构体内存对齐方式的总结(#pragma pack()和alignas())

    最近闲来无事,翻阅msdn,在预编译指令中,翻阅到#pragma pack这个预处理指令,这个预处理指令为结构体内存对齐指令,偶然发现还有另外的内存对齐指令aligns(C++11),__declsp ...

  4. [C/C++] 结构体内存对齐用法

    一.为什么要内存对齐 经过内存对齐之后,CPU的内存访问速度大大提升; 内存空间按照byte划分,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定变量的时候经常在特定的内 ...

  5. [C/C++] 结构体内存对齐:alignas alignof pack

    简述: alignas(x):指定结构体内某个成员的对齐字节数,指定的对齐字节数不能小于它原本的字节数,且为2^n; #pragma pack(x):指定结构体的对齐方式,只能缩小结构体的对齐数,且为 ...

  6. go语言结构体内存对齐

    cpu要想从内存读取数据,需要通过地址总线,把地址传输给内存,内存准备好数据,输出到数据总线,交给cpu,如果地址总线只有8根,那这个地址就只有8位可以表示[0,255]256个地址,因为表示不了更多 ...

  7. C语言-结构体内存对齐

    C语言结构体对齐也是老生常谈的话题了.基本上是面试题的必考题.内容虽然很基础,但一不小心就会弄错.写出一个struct,然后sizeof,你会不会经常对结果感到奇怪?sizeof的结果往往都比你声明的 ...

  8. c 结构体内存对齐详解

    0x00简介 首先要知道结构体的对齐规制 1.第一个成员在结构体变量偏移量为0的地址处 2.其他成员变量对齐到某个数字的整数倍的地址处 对齐数=编辑器默认的一个对齐数与该成员大小的较小值 vs中默认的 ...

  9. C语言中结构体内存对齐

    先写一个小程序: #include<stdio.h> struct student  {    int a;   char k;   short m; }; int main() { st ...

随机推荐

  1. CDDA 源码解析

    一.编译1:从 https://github.com/CleverRaven/Cataclysm-DDA 下载源码2:下载IDE CodeBlocks,http://pan.baidu.com/s/1 ...

  2. Oracle初级函数的使用

    --1.字符函数--UPPER(string|column) 可以将字符转成大写select upper('helloword') from dual;select upper(ename) from ...

  3. 第一次做socket的一些心得

    理论什么的bilibala的就是自己百度吧 推荐一篇不错的关于socket的文章 http://www.cnblogs.com/sunway/archive/2010/01/29/1659074.ht ...

  4. CH模拟赛 还教室

    /* 区间操作,可以推一推式子,方差为平方的平均数-平均数的平方,维护区间和与区间平方和,平方和的维护方法类似,式子推一推就行了,注意约分 */ #include<iostream> #i ...

  5. GitHub上值得关注的iOS开源项目

    1.AFNetworking地址:https://github.com/AFNetworking/AFNetworking用于网络请求 2.JSONKit地址:https://github.com/j ...

  6. MySQL收藏

    MySQL手册:5.5手册 .5.6手册 cd /usr/local/mysql/bin mysql -uroot -p show processlist;   // 显示系统中正在运行的所有进程 M ...

  7. Java获取某年第一天和最后一天

    package com.dada.test; import java.text.SimpleDateFormat; import java.util.Calendar; import java.uti ...

  8. Windows7安装 .net framework 4.0

    1.首先下载安装包.net framework 4.0点击下载即可 2.安装,双击下载好的安装包

  9. 使用svn分支

    使用svn分支 原文: https://my.oschina.net/cobish/blog/652984

  10. matlab -xlsread 打开xls文件出错:服务器出现意外情况

    错误:xlsread:服务器出现意外情况 原因:非matlab问题,Excel的com加载项启用 解决方法:office - Excel选项-加载项:管理-com加载项-转到-取消可用加载项的勾选.