字節長度,字節對齊以及類,對象的長度
?
//
?sizeof.cpp?:?Defines?the?entry?point?for?the?console?application.
//
#include?
"
stdafx.h
"
#include??
<
iostream
>
using
?
namespace
?std;
struct
?st?
????
{
????????
short
?num;
????????
float
?math_grade;
????????
float
?Chinese_grade;
????????
float
?sum_grade;
????}
;
void
?main()
{
????cout
<<
"
sizeof('$')=
"
<<
sizeof
(
'
$
'
)
<<
endl;????
????cout
<<
"
sizeof(1)=
"
<<
sizeof
(
1
)
<<
endl;
????cout
<<
"
sizeof(1.5)=
"
<<
sizeof
(
1.5
)
<<
endl;??
????cout
<<
"
sizeof(Good!)=
"
;
????cout
<<
sizeof
(
"
Good!
"
)
<<
endl;?
????
int
?i
=
100
;
????
char
?c
=
'
A
'
;
????
float
?x
=
3.1416
;?
????
double
?p
=
0.1
;
????cout
<<
"
sizeof(i)=
"
<<
sizeof
(i)
<<
endl;
????cout
<<
"
sizeof(c)=
"
<<
sizeof
(c)
<<
endl;
????cout
<<
"
sizeof(x)=
"
<<
sizeof
(x)
<<
endl;
????cout
<<
"
sizeof(p)=
"
<<
sizeof
(p)
<<
endl;?
????cout
<<
"
sizeof(x+1.732)=
"
<<
sizeof
(x
+
1.732
)
<<
endl;
????cout
<<
"
sizeof(char)=
"
<<
sizeof
(
char
)
<<
endl;
????cout
<<
"
sizeof(int)=
"
<<
sizeof
(
int
)
<<
endl;
????cout
<<
"
sizeof(float)=
"
<<
sizeof
(
float
)
<<
endl;
????cout
<<
"
sizeof(double)=
"
<<
sizeof
(
double
)
<<
endl;
????
char
?str[]
=
"
This?is?a?test.
"
;
????
int
?a[
10
];?
????
double
?xy[
10
];
????cout
<<
"
sizeof(str)=
"
<<
sizeof
(str)
<<
endl;
????cout
<<
"
sizeof(a)=
"
<<
sizeof
(a)
<<
endl;
????cout
<<
"
sizeof(xy)=
"
<<
?
sizeof
(xy)
<<
endl;
????
????st?student1;
????cout
<<
"
sizeof(st)=
"
<<
sizeof
(st)
<<
endl;
????cout
<<
"
sizeof(student1)=
"
<<
sizeof
(student1);
}
----------------------------the result are:-------------------------------------
sizeof('$')=1
sizeof(1)=4
sizeof(1.5)=8
sizeof("Good!")=6
sizeof(i)=4
sizeof(c)=1
sizeof(x)=4
sizeof(p)=8
sizeof(x+1.732)=8
sizeof(char)=1
sizeof(int)=4
sizeof(float)=4
sizeof(double)=8
sizeof(str)=16
sizeof(a)=40
sizeof(xy)=80
sizeof(st)=16
sizeof(student1)=16
Press any key to continue
-------------------------------------------------------------------------
//? #pragma pack( )?
//? mulbayes?
//? unicode
-------------------------------------------------------------------------
為了能使CPU對變量進行高效快速的訪問,變量的起始地址應該具有某些特性,
即所謂的“對齊”。例如對于4字節的int類型變量,其起始地址應位于4字節邊界上,
即起始地址能夠被4整除。變量的對齊規則如下(32位系統):
Type
Alignment
char
在字節邊界上對齊
short (16-bit)
在雙字節邊界上對齊
int and long (32-bit)
在4字節邊界上對齊
float
在4字節邊界上對齊
double
在8字節邊界上對齊
?
structures
單獨考慮結構體的個成員,它們在不同的字節邊界上對齊。
其中最大的字節邊界數就是該結構的字節邊界數。
MSDN原話:Largest alignment requirement of any member
理解結構體的對齊方式有點撓頭,如果結構體中有結構體成員,
那么這是一個遞歸的過程。
對齊方式影響結構體成員在結構體中的偏移設編譯器設定的最大對齊字節邊界數為n,
對于結構體中的某一成員item,它相對于結構首地址的實際字節對齊數目X應該滿足
以下規則:
X = min(n, sizeof(item))
例如,對于結構體 struct {char a; int b} T;
當位于32位系統,n=8時:
a的偏移為0,
b的偏移為4,中間填充了3個字節, b的X為4;
當位于32位系統,n=2時:
a的偏移為0,
b的偏移為2,中間填充了1個字節,b的X為2;
結構體的sizeof
設結構體的最后一個成員為LastItem,其相對于結構體首地址的
偏移為offset(LastItem),其大小為sizeof(LastItem),結構體的字節對齊數為N,
則:結構體的sizeof 為: 若offset(LastItem)+ sizeof(LastItem)能夠被N整除,
那么就是offset(LastItem)+ sizeof(LastItem),否則,在后面填充,
直到能夠被N整除。
例如:32位系統,n=8,
結構體 struct {char a; char b;} T;
struct {char a; int b;} T1;
struct {char a; int b; char c;} T2;
sizeof(T) == 2; N = 1 沒有填充
sizeof(T) == 8; N = 4 中間填充了3字節
sizeof(T2)==12; N = 4 中間,結尾各填充了3字節
注意:
1) 對于空結構體,sizeof == 1;因為必須保證結構體的每一個實例在內存中都
有獨一無二的地址。
2) 結構體的靜態成員不對結構體的大小產生影響,因為靜態變量的存儲位置與
結構體的實例地址無關。
例如:
struct {static int I;} T; struct {char a; static int I;} T1;
sizeof(T) == 1; sizeof(T1) == 1;
3) 某些編譯器支持擴展指令設置變量或結構的對齊方式,如VC,
? 詳見MSDN(alignment of structures)
?
并不是要求#pragma pack(8),就一定是每個成員都是8字節對齊
而是指一組成員要按照8字節對齊。
struct s1
{
?? short a;?? // 2字節
?? long b;??? // 4字節
};
整個s1小于8字節,因此s1就是8字節。
struct s2
{
?? char c;??? // 1字節
?? s1 d;????? // 8字節
?? __int64 e; // 8字節
};
整個s2小于12字節,但是由于#pragma pack(8)的限定,12不能與8字節對齊,因此s2就是24字節,c占用8字節
---------------------------------
類或對象的長度:
?? 非虛函數相當與全局,不在類里。
?? 靜態也是全局,不在類里。
?? 但是const要分配空間。
非靜態變量,虛函數鏈表(如果類中有虛函數的話) -----------分配空間
posted on 2005-10-24 17:39 夢在天涯 閱讀(3732) 評論(4) 編輯 收藏 引用 所屬分類: CPlusPlus

