Home > AI > Uncategorized

struct对齐

#include <stdio.h>
#include <stdlib.h>

//32位的CPU内存以4字节对齐,64位以8字节对齐
//gcc默认4

#pragma pack(8) //没用
typedef struct _parent {
    int data;
} parent; //4

typedef struct _child {
    struct _parent parent; //4
    int data; //4
} child; //8

typedef struct _test {
    double c; //8
    char b; //1
    int a; //4
} test; //16


int main(int argc, char *argv[])
{
    printf("parent %lu, child %lu, test %lu\n", sizeof(parent), sizeof(child), sizeof(test));
}

 

Related posts:

Leave a Reply