forked from selfboot/CS_Offer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
C++_Memory_Align.cpp
48 lines (42 loc) · 931 Bytes
/
C++_Memory_Align.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* @Author: xuezaigds@gmail.com
* @Last Modified time: 2016-03-01 14:47:56
*/
#include <iostream>
using namespace std;
struct align_default
{
char x1;
double x2;
short x3;
int x4;
};
#pragma pack(1) // 设定对齐系数为1
struct align_1
{
char x1;
double x2;
short x3;
int x4;
};
#pragma pack()
#pragma pack(4) // 设定对齐系数为4
struct align_4
{
char x1;
double x2;
short x3;
int x4;
};
#pragma pack()
int main()
{
cout<<"sizeof(char) : "<<sizeof(char)<<endl; // 1byte
cout<<"sizeof(short) : "<<sizeof(short)<<endl; // 2bytes
cout<<"sizeof(int) : "<<sizeof(int)<<endl; // 4bytes
cout<<"sizeof(double): "<<sizeof(double)<<endl; // 8bytes
cout << sizeof(align_default) << endl; // 24
cout << sizeof(align_1) << endl; // 15
cout << sizeof(align_4) << endl; // 20
return 0;
}