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*)=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
时间: 2024-10-03 14:02:21

C++结构体内存对齐跨平台测试的相关文章

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

17.1 Introduction 这一章主要讲了UNIX Domain Sockets这样的进程间通讯方式,并列举了具体的几个例子. 17.2 UNIX Domain Sockets 这是一种特殊socket类型,主要用于高效的IPC,特点主要在于高效(因为省去了很多与数据无关的格式的要求). int socketpair(int domain, int type, int protocol, int sockfd[2]) 这个函数用于构建一对unix domain sockets:并且与之前

C++ Primer 学习笔记_15_从C到C++(1)--bool类型、const限定符、const与#define、结构体内存对齐

欢迎大家阅读参考,如有错误或疑问请留言纠正,谢谢 一.bool类型(C语言没有) 1.逻辑型也称布尔型,其取值为true(逻辑真)和false(逻辑假),存储字节数在不同编译系统中可能有所不同,VC++中为1个字节. 2.声明方式:bool result; result=true; 3.可以当作整数用(true一般为1,false为0) 4.把其它类型的值转换为布尔值时,非零值转换为true,零值转换为false 5.示例 #include <iostream> using namespace

结构体内存对齐规则

结构体内存对齐规则 1.第一个成员在与结构体变量偏移量为0的地址处: 2.其他成员变量要对齐到某个数字(对齐数)的整数倍的地址处: 对齐数=编译器默认的对齐数 与 该成员大小的较小值: vc中默认为对齐数为8 linux中默认对齐数为4 3.结构体总大小为最大对齐数的整数倍: 4.如果嵌套了结构体,嵌套的结构体对齐到自己的最大对齐数的整数倍处,结构体 的整体大小就是所有最大对齐数(含嵌套结构体的对齐数)的整数倍 如下面代码 struct A { int a;//4  8   ->4 char c

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

最近闲来无事,翻阅msdn,在预编译指令中,翻阅到#pragma pack这个预处理指令,这个预处理指令为结构体内存对齐指令,偶然发现还有另外的内存对齐指令aligns(C++11),__declspec(align(#))(Microsoft专用),遂去探究两者之间的不同点. 1.#pragma pack 这个指令为预处理指令,所谓与处理指令执行在程序的预处理阶段,该指令对应着编译选项/Zp,可以在vs的工程属性中设置编译选项的内存对齐,也可以利用预处理指令来设置. #pragma pack(

关于结构体内存对齐

对齐规则如下: 1.  结构体的大小等于结构体内最大成员大小的整数倍2.  结构体内的成员的首地址相对于结构体首地址的偏移量是其类型大小的整数倍,比如说double型成员相对于结构体的首地址的地址偏移量应该是8的倍数.3.  为了满足规则1和2编译器会在结构体成员之后进行字节填充! 注: 1. 内存对齐只是一种用空间来换时间的方法. 2. 实际上不同的编译器他的优化规则并不完全相同.

结构体内存对齐

1.原因:为什么需要内存对齐.1.平台原因(移植原因):不是所有的硬件平台都能访问任意地址上的任意数据的:某些硬件平台只能在某些地址处取某些特定类型的数据,否则抛出硬件异常. 2.性能原因:数据结构(尤其是栈)应该尽可能地在自然边界上对齐.原因在于,为了访问未对齐的内存,处理器需要作两次内存访问:而对齐的内存访问仅需要一次访问. 2.内存对齐的规则和范例讲述内存对齐之前先看下各种类型的大小,和编译器以及字长有关具体在此不多叙述.成员的内存分配规律:从结构体的首地址开始向后依次为每个成员寻找第一个

跟我一起学C++之从C到C++(结构体内存对齐)

1.什么是内存对齐 (1)      编译器为每个"数据单元"按排在某个合适的位置上. (2)      C.C++语言非常灵活,它允许你干涉"内存对齐".也就是可以人为的设置编译器的对齐方式. 2.为什么要对齐 性能原因:在对齐的地址上访问数据快.如果是字节对齐方式存储的话,CPU读取的时候只需要进行一个总线周期即可全部读取完毕,如果不对齐的话,对于32位的系统,CPU读取的时候一般架构都是从偶地址开启的,一次只能读取4个字节,因此需要两个总线周期才能完成,并且还

C/C++结构体内存对齐

ZC: 注意点:ZC: (1).不同的编译器 对结构体("struct"和"typedef struct")的内存对齐 可能不同:ZC: (2).关键词"__packed",网上查到 可以使用这个,但是我没有尝试成功,不知 如何使用... 测试环境:Win7x64,cn_visual_studio_2010_ultimate_x86_dvd_532347.iso,qt-opensource-windows-x86-msvc2010_opengl-

结构体内存对齐详解

.在32位机器上 设有以下说明和定义: 1234567891011 typedef union { long i; int k[5]; char c; } DATE; struct data { int cat; DATE cow; double dog; } too; DATE max; 则语句 printf("%d",sizeof(struct data)+sizeof(max));的执行结果是: (答案:52) 这道题可能很多同学都有误区,不明白内存对齐的真正含义,那么下面我来讲