For a lot of C
novice, objects or variables
SIZEOF information is always unpredictable, and the following procedures list a few sets of typical
SizeOf information, I hope to answer everyone in use
Question in SIZEOF.
In the case, you need to explain the following points before the example:
1. On the Win32 platform, the length of the pointer is
4 bytes,
charr
*,
int
*,
Double
* This, Vbptr
(
Virtual base Table Pointer
), VFPTR
(
Virtual Function Table Pointer
)is also like this;
2, for structures
(Or class)
), The compiler automatically performs alignment of member variables to improve operational efficiency. Natural alignment
(Natural Alignment
The default alignment is aligned in Size's largest member in a member of the structure, and forced designation is not a role of alignment of natural alignment sizes.
(Specifying a Packing Level Larger Than THE Natural Alignment of a Type Does
NOT CHANGE The TYPE Alignment
.
3. It is not recommended to force alignment, a large number of forced alignment will seriously affect the processing efficiency of the processor.
example
1:
(A simple example of C language
)
Void F
(
Int Arr
[]) {
cout
<<
"SIZEOF (ARR) ="
<<
Sizeof
(Arr
<< ENDL
;
// When it is passed as a parameter, the array lost its size information.
}
Void
main
() {
Char szbuf
[] =
"ABC"
;
cout
<<
"SIZEOF (SZBUF) ="
<<
Sizeof
(SZBUF
<< ENDL
;
// Output array occupied space size
charr
* Pszbuf
= SZBUF
;
cout
<<
"SIZEOF (PSZBUF) ="
<<
Sizeof
(pszbuf
<< ENDL
;
// Output is the size of the pointer
Int IARR
[
3
]; IARR
;
cout
<<
"SIZEOF (IARR) ="
<<
Sizeof
(IARR
<< ENDL
;
/ / Output array occupied space size f
(IARR
);
int
* Piarr
= IARR
;
cout
<<
"SIZEOF (PIARR) ="
<<
Sizeof
(Piarr
<< ENDL
;
// Output the size of the pointer
}
example
2:
(A example involving alignment
)
Struct Data1
{
Char C1
;
// Offset 0, accumulated SIZE = 1
Char C2
;
// Offset 1, accumulated SIZE = 1 1 = 2
Short Si
;
// Offset 2, accumulated SIZE = 2 2
}
Struct Data2
{
Char C1
;
// Offset 0, accumulated SIZE = 1
Short Si
;
// Offset 1 (1), cumulative SIZE = 1 (1) 2 = 4CHAR C2
;
// Offset size 4 1 = 5, but according to the maximum length sizeof (Short) = 2 alignment, the last taking 6
}
Struct Data3
{
Char C1
;
// Offset 0, accumulated SIZE = 1
Double D
;
// Offset 1 (7), cumulative SIZE = 1 (7) 8 = 16
Char C2
;
// Offset 16, cumulative size = 16 1 = 17, but according to the maximum length sizeof (double) = 8 alignment, the last taking 24
}
#pragma pack (push, 1) // Force 1 byte alignment
Struct Data4
{
Char C1
;
// Offset 0, accumulated SIZE = 1
Double D
;
// Offset 1, accumulated SIZE = 1 8 = 9
Char C2
;
// Offset 9, accumulated SIZE = 9 1 = 10
}
#pragma pack (pop) // Restore default alignment
Struct Data5
{
Char C1
;
Double D
;
Char C2
};
Void
main
() {
cout
<<
"SIZEOF (DATA1) ="
<<
Sizeof
(Data1
<< ENDL
;
cout
<<
"SIZEOF (DATA2) ="
<<
Sizeof
(Data2
<< ENDL
;
cout
<<
"SIZEOF (DATA3) ="
<<
Sizeof
(Data3
<< ENDL
;
cout
<<
"SIZEOF (DATA4) ="
<<
Sizeof
(Data4
<< ENDL
;
cout
<<
"SIZEOF (DATA5) ="
<<
Sizeof
(Data5
<< ENDL
}
example
3:
(C
language characteristics
Influence of SIZEOF
)
Class CA
{};
Class CB
:
Public CA
{
public
:
Void Func
() {}};
Class CC
:
Virtual Public CA
{};
Class CD
{
Int K
;
// Private member
public
:
CD
() {K
= -
1
}
Void Printk
() {Cout
<<
"k ="
<< k
<< ENDL
;}};
Class CE
:
PUBLIC CD
{};
Class CF
{
Virtual
Void Func
() {}};
Void
main
() {
cout
<<
"SIZEOF (CA) ="
<<
Sizeof
(CA
<< ENDL
;
/ / In order to distinguish the different elements of the class that does not contain any members, the compiler will automatically add an anonymous element to the class COUT <<
"SIZEOF (CB) ="
<<
Sizeof
(CB
<< ENDL
;
// Similar to the above, the compiler also adds an anonymous element to CB
<<
"SIZEOF (CC) ="
<<
Sizeof
(CC
<< ENDL
;
// VBPTR (Virtual Base Table Pointer) occupies 4 bytes in virtual inheritance
cout
<<
"SIZEOF (CD) ="
<<
Sizeof
(CD)
<< ENDL
;
cout
<<
"SIZEOF (CE) ="
<<
Sizeof
(CE
<< ENDL
;
// Access Permissions Control is controlled by the compiler during compilation, so although the member K of the CD class cannot be accessed, the code below the SIZEOF (int) size // further illustrates the above view, due to complex In the class hierarchy, when it comes to virtual functions or virtual inheritance, some information is dynamically generated by running period, so please do not follow the following methods to modify the object to CE E
;
e
.printk
();
MEMSET
(& E
,
0
,
Sizeof
(CE
));
e
.printk
();
// From here you can see that the above MEMSET operation modifies the private member K of the CD class.
cout
<<
"SIZEOF (CF) ="
<<
Sizeof
(CF
<< ENDL
;
// virtual function table pointer occupies 4 bytes
}