标签:form style maximum represent data- present pack rate str
attribute method:
#include <stdio.h>
struct packed
{
    char a;
    int b;
} __attribute__((packed));
struct not_packed
{
    char a;
    int b;
};
int main(void)
{
    printf("Packed:     %zu\n", sizeof(struct packed));
    printf("Not Packed: %zu\n", sizeof(struct not_packed));
    return 0;
}Output:
$ make example && ./example
cc     example.c   -o example
Packed:     5
Not Packed: 8pragma pack method:
#include <stdio.h>
#pragma pack(1)
struct packed
{
    char a;
    int b;
};
#pragma pack()
struct not_packed
{
    char a;
    int b;
};
int main(void)
{
    printf("Packed:     %zu\n", sizeof(struct packed));
    printf("Not Packed: %zu\n", sizeof(struct not_packed));
    return 0;
}Output:
$ make example && ./example
cc     example.c   -o example
Packed:     5
Not Packed: 8Add -fpack-struct to GCC
Warning: the -fpack-struct switch causes GCC to generate code that is not binary compatible with code generated without thatswitch. Additionally, it makes the code suboptimal. Use it to conform to a non-default application binary interface.
标签:form style maximum represent data- present pack rate str
原文地址:http://www.cnblogs.com/wzjhoutai/p/7201795.html