数据对齐问题

#include<stdio.h>

typedef struct node1
{
    short   s; // 2 bytes
    double  d; // 8 bytes
    char    c; // 1 bytes
}A1;

typedef struct node2
{
    char   c; // 1 bytes
    short  s; // 2 bytes
    double d; // 8 bytes
}A2;

void main()
{
    printf("%d\n", sizeof (A1)); // 结果 24
    printf("%d\n", sizeof (A2)); // 结果 16

    getchar();
}

A1和A2结构体中, 元素一模一样, 只是排列顺序不一样, 为什么大小会发生变化? 这就是对数据对齐问题的一个简单的思考.

A2在数据对齐前后的差别:

原文地址:https://www.cnblogs.com/rookie2/p/2774777.html