#include <stdio.h>
struct A1
{
};
struct A2
{
;
};
struct A3
{
char a;
int b;
unsigned short c;
long d;
unsigned long long e;
char f;
};
struct A4
{
int b;
char a;
unsigned short c;
long d;
unsigned long long e;
char f;
};
#pragma pack(2)
struct B1
{
char a;
int b;
unsigned short c;
long d;
unsigned long long e;
char f;
};
#pragma pack()
#pragma pack(4)
struct B2
{
char a;
int b;
unsigned short c;
long d;
unsigned long long e;
char f;
};
#pragma pack()
#pragma pack(8)
struct B3
{
char a;
int b;
unsigned short c;
long d;
unsigned long long e;
char f;
};
#pragma pack()
#pragma pack(16)
struct B4
{
char a;
int b;
unsigned short c;
long d;
unsigned long long e;
char f;
};
#pragma pack()
struct C1
{
char a;
int b;
unsigned short c;
long d;
unsigned long long e;
char f;
} __attribute__((aligned));
struct C2
{
char a;
int b;
unsigned short c;
char f;
} __attribute__((aligned));
struct C3
{
char a;
int b;
unsigned short c;
long d;
char f;
} __attribute__((aligned));
struct C4
{
char a;
unsigned short c;
unsigned long long e;
char f;
} __attribute__((aligned));
struct C5
{
char a;
} __attribute__((aligned));
struct D1
{
char a;
int b;
unsigned short c;
long d;
unsigned long long e;
char f;
} __attribute__((aligned(1)));
struct D2
{
char a;
int b;
unsigned short c;
long d;
unsigned long long e;
char f;
} __attribute__((aligned(4)));
struct D3
{
char a;
int b;
unsigned short c;
long d;
unsigned long long e;
char f;
} __attribute__((aligned(8)));
struct D4
{
char a;
int b;
unsigned short c;
long d;
unsigned long long e;
char f;
} __attribute__((aligned(16)));
struct E1
{
char a;
int b;
unsigned short c;
long d;
unsigned long long e;
char f;
} __attribute__((packed));
int main()
{
printf("sizeof(char):%2d...sizeof(int):%2d...sizeof(ushort):%2d...sizeof(long):%2d...sizeof(ulonglong):%2d\n\r",
sizeof(char), sizeof(int), sizeof(unsigned short), sizeof(long), sizeof(unsigned long long));
printf("sizeof(A1):%2d...sizeof(A2):%2d...sizeof(A3):%2d...sizeof(A4):%2d\n\r",
sizeof(struct A1), sizeof(struct A2), sizeof(struct A3), sizeof(struct A4));
printf("sizeof(B1):%2d...sizeof(B2):%2d...sizeof(B3):%2d...sizeof(B4):%2d\n\r",
sizeof(struct B1), sizeof(struct B2), sizeof(struct B3), sizeof(struct B4));
printf("sizeof(C1):%2d...sizeof(C2):%2d...sizeof(C3):%2d...sizeof(C4):%2d...sizeof(C5):%2d\n\r",
sizeof(struct C1), sizeof(struct C2), sizeof(struct C3), sizeof(struct C4), sizeof(struct C5));
printf("sizeof(D1):%2d...sizeof(D2):%2d...sizeof(D3):%2d...sizeof(D4):%2d\n\r",
sizeof(struct D1), sizeof(struct D2), sizeof(struct D3), sizeof(struct D4));
printf("sizeof(E1):%2d...\n\r", sizeof(struct E1));
return 1;
}
g++编译输出结果如下:
sizeof(char): 1...sizeof(int): 4...sizeof(ushort): 2...sizeof(long): 8...sizeof(ulonglong): 8
sizeof(A1): 1...sizeof(A2): 1...sizeof(A3):40...sizeof(A4):32
sizeof(B1):26...sizeof(B2):32...sizeof(B3):40...sizeof(B4):40
sizeof(C1):48...sizeof(C2):16...sizeof(C3):32...sizeof(C4):32...sizeof(C5):16
sizeof(D1):40...sizeof(D2):40...sizeof(D3):40...sizeof(D4):48
sizeof(E1):24...
评论