首页 > 代码库 > C++结构体内存对齐跨平台测试

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 0;}

Output msvc x86:

sizeof(void*)=4sizeof(long long)=8sizeof(Test8)=24sizeof(Tes4)=16sizeof(Test1)=10sizeof(Test0)=24

Output msvc x64:

sizeof(void*)=8sizeof(long long)=8sizeof(Test8)=24sizeof(Tes4)=16sizeof(Test1)=10sizeof(Test0)=24

Output xcode IOS armV7:

sizeof(void*)=4sizeof(long long)=8sizeof(Test8)=16sizeof(Tes4)=16sizeof(Test1)=10sizeof(Test0)=16

Output xcode IOS arm64:

sizeof(void*)=8sizeof(long long)=8sizeof(Test8)=24sizeof(Tes4)=16sizeof(Test1)=10sizeof(Test0)=24

 

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*)=4D/JNITag: sizeof(long long)=8D/JNITag: sizeof(Test8)=16D/JNITag: sizeof(Tes4)=16D/JNITag: sizeof(Test1)=10D/JNITag: sizeof(Test0)=16

 

测试二,简单数组测试,源码:

#include <stdio.h>#pragma pack(push)#pragma pack(8)struct Test8{    char a[3];};#pragma pack(pop)#pragma pack(push)#pragma pack(4)struct Test4{    char a[3];};#pragma pack(pop)#pragma pack(push)#pragma pack(1)struct Test1{    char a[3];};#pragma pack(pop)struct Test0{    char a[3];};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 0;}

 

msvc x86:

sizeof(void*)=4sizeof(long long)=8sizeof(Test8)=3sizeof(Tes4)=3sizeof(Test1)=3sizeof(Test0)=3

msvc x64:

sizeof(void*)=8sizeof(long long)=8sizeof(Test8)=3sizeof(Tes4)=3sizeof(Test1)=3sizeof(Test1)=3

iphone armv7:

sizeof(void*)=4sizeof(long long)=8sizeof(Test8)=3sizeof(Tes4)=3sizeof(Test1)=3sizeof(Test0)=3

iphone arm64:

sizeof(void*)=8sizeof(long long)=8sizeof(Test8)=3sizeof(Tes4)=3sizeof(Test1)=3sizeof(Test0)=3

android armv7 simulator:

D/JNITag: sizeof(void*)=4D/JNITag: sizeof(long long)=8D/JNITag: sizeof(Test8)=3D/JNITag: sizeof(Tes4)=3D/JNITag: sizeof(Test1)=3D/JNITag: sizeof(Test0)=3

 

测试三,当结构体包含pack1的结构体时

code:

#include <stdio.h>#include <stddef.h>#pragma pack(push)#pragma pack(1)struct Test1{    char a[3];    long long b;    char d[2];    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 0;}

 

msvc x86:

sizeof(void*)=4sizeof(long long)=8sizeof(Test1)=17---------------------------------------sizeof(Owner8)=24offsetof(Owner8, a)=1offsetof(Owner8, i2)=20---------------------------------------sizeof(Owner4)=24offsetof(Owner4, a)=1offsetof(Owner4, i2)=20---------------------------------------sizeof(Owner1)=22offsetof(Owner1, a)=1offsetof(Owner1, i2)=18---------------------------------------sizeof(Owner0)=24offsetof(Owner0, a)=1offsetof(Owner0, i2)=20

msvc x64:

sizeof(void*)=8sizeof(long long)=8sizeof(Test1)=17---------------------------------------sizeof(Owner8)=24offsetof(Owner8, a)=1offsetof(Owner8, i2)=20---------------------------------------sizeof(Owner4)=24offsetof(Owner4, a)=1offsetof(Owner4, i2)=20---------------------------------------sizeof(Owner1)=22offsetof(Owner1, a)=1offsetof(Owner1, i2)=18---------------------------------------sizeof(Owner0)=24offsetof(Owner0, a)=1offsetof(Owner0, i2)=20

xcode ios armv7:

sizeof(void*)=4sizeof(long long)=8sizeof(Test1)=17---------------------------------------sizeof(Owner8)=24offsetof(Owner8, a)=1offsetof(Owner8, i2)=20---------------------------------------sizeof(Owner4)=24offsetof(Owner4, a)=1offsetof(Owner4, i2)=20---------------------------------------sizeof(Owner1)=22offsetof(Owner1, a)=1offsetof(Owner1, i2)=18---------------------------------------sizeof(Owner0)=24offsetof(Owner0, a)=1offsetof(Owner0, i2)=20

 

xcode ios arm64:

sizeof(void*)=8sizeof(long long)=8sizeof(Test1)=17---------------------------------------sizeof(Owner8)=24offsetof(Owner8, a)=1offsetof(Owner8, i2)=20---------------------------------------sizeof(Owner4)=24offsetof(Owner4, a)=1offsetof(Owner4, i2)=20---------------------------------------sizeof(Owner1)=22offsetof(Owner1, a)=1offsetof(Owner1, i2)=18---------------------------------------sizeof(Owner0)=24offsetof(Owner0, a)=1offsetof(Owner0, i2)=20

android armv7 simulator:

D/JNITag: sizeof(void*)=4D/JNITag: sizeof(long long)=8D/JNITag: sizeof(Test1)=17D/JNITag: ---------------------------------------D/JNITag: sizeof(Owner8)=24D/JNITag: offsetof(Owner8, a)=1D/JNITag: offsetof(Owner8, i2)=20D/JNITag: ---------------------------------------D/JNITag: sizeof(Owner4)=24D/JNITag: offsetof(Owner4, a)=1D/JNITag: offsetof(Owner4, i2)=20D/JNITag: ---------------------------------------D/JNITag: sizeof(Owner1)=22D/JNITag: offsetof(Owner1, a)=1D/JNITag: offsetof(Owner1, i2)=18D/JNITag: ---------------------------------------D/JNITag: sizeof(Owner0)=24D/JNITag: offsetof(Owner0, a)=1D/JNITag: offsetof(Owner0, i2)=20

 

C++结构体内存对齐跨平台测试