码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-10-23 14:38:44      阅读:432      评论:0      收藏:0      [点我收藏+]

标签:ack   long   规则   数组   sizeof   简单   iphone   android   void   

测试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*)=4
sizeof(long long)=8
sizeof(Test8)=24
sizeof(Tes4)=16
sizeof(Test1)=10
sizeof(Test0)=24

Output msvc x64:

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

Output xcode IOS armV7:

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

Output xcode IOS arm64:

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

msvc x64:

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

iphone armv7:

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

iphone arm64:

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

android armv7 simulator:

D/JNITag: sizeof(void*)=4
D/JNITag: sizeof(long long)=8
D/JNITag: sizeof(Test8)=3
D/JNITag: sizeof(Tes4)=3
D/JNITag: sizeof(Test1)=3
D/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*)=4
sizeof(long long)=8
sizeof(Test1)=17
---------------------------------------
sizeof(Owner8)=24
offsetof(Owner8, a)=1
offsetof(Owner8, i2)=20
---------------------------------------
sizeof(Owner4)=24
offsetof(Owner4, a)=1
offsetof(Owner4, i2)=20
---------------------------------------
sizeof(Owner1)=22
offsetof(Owner1, a)=1
offsetof(Owner1, i2)=18
---------------------------------------
sizeof(Owner0)=24
offsetof(Owner0, a)=1
offsetof(Owner0, i2)=20

msvc x64:

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

xcode ios armv7:

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

 

xcode ios arm64:

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

android armv7 simulator:

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

 

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

标签:ack   long   规则   数组   sizeof   简单   iphone   android   void   

原文地址:http://www.cnblogs.com/bodong/p/5989297.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!