C ++ byte length, byte alignment, class, object Length

Source: Internet
Author: User
Byte Length, byte alignment, class, object Length
struct  st {short  num;float  math_grade;float  Chinese_grade;float  sum_grade;};int _tmain(int argc, _TCHAR* argv[]){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);return 0;}

 

---------------------------- The result are :-------------------------------------

 

To enable the CPU to efficiently and quickly access variables, the starting address of the variable should have some characteristics, that is, the so-called "alignment ". For example, for a 4-byte int type variable, its starting address should be located on the 4-byte boundary, that is, the starting address can be divisible by 4. The variable alignment rules are as follows (32-bit system ):

Type alignment

Char alignment on Byte Boundary

Short (16-bit) alignment on the dual-byte Boundary

INT and long (32-bit) are aligned on the 4-byte Boundary

Float is aligned on the 4-byte Boundary

Double is aligned on the 8-byte Boundary

 

Structures considers the individual members of the struct, which are aligned on different byte boundaries. The maximum number of boundary bytes is the number of boundary bytes of the structure. In the original words of msdn: largest alignment requirement of any member, understanding the alignment of a struct is a bit scratching its head. If the struct contains struct Members, this is a recursive process. Alignment affects the offset of struct members in the struct. the maximum number of alignment byte Boundary set by the compiler is N. For a member item in the struct, it is aligned with the actual number of bytes at the beginning of the structure X should meet the following rules:

X = min (n, sizeof (item ))

For example, for struct {char a; int B} t;

When the system is 32-bit, n = 8: The offset of A is 0, the offset of B is 4, 3 bytes are filled in the middle, and X of B is 4;

In a 32-bit system, if n = 2: The offset of A is 0, the offset of B is 2, 1 byte is filled in the middle, and X of B is 2;

The sizeof of struct is set to lastitem as the last member of the struct, and its offset relative to the first address of the struct is offset (lastitem). Its size is sizeof (lastitem), and the bytes of the struct are aligned to n, then: the sizeof of struct is: If offset (lastitem) + sizeof (lastitem) can be divisible by N, It is offset (lastitem) + sizeof (lastitem). Otherwise, it will be filled later, until it can be divisible by N.

Example: 32-bit system, n = 8, struct {char a; char B;} t; struct {char a; int B;} T1; struct {char; int B; char C;} T2; sizeof (t) = 2; n = 1 No sizeof (t) = 8; 3 bytes in the middle of N = 4 sizeof (T2) = 12; n = 4, and 3 bytes in the end

Note:

1) For an empty struct, sizeof = 1, because each instance of the struct must have a unique address in the memory.

2) The static member of the struct does not affect the size of the struct because the storage location of the static variable is irrelevant to the instance address of the struct.

For example:

Struct {static int I ;}t; struct {char a; static int I ;}t1; sizeof (t) = 1; sizeof (T1) = 1;

3) Some compilers support extension instructions to set the alignment of variables or structures, such as VC. For details, see msdn (alignment of structures)

 

# Pragma pack (8) is not required. Each member must be 8-byte aligned, but a group of Members must be 8-byte aligned. Struct S1 {short a; // 2 bytes long B; // 4 bytes}; the whole S1 is smaller than 8 bytes, so S1 is 8 bytes.

Struct S2 {char C; // 1 byte S1 D; // 8 byte _ int64 E; // 8 byte}; the entire S2 is less than 12 bytes, however, due to the limitation of # pragma pack (8), 12 cannot be aligned with 8 bytes, so S2 is 24 bytes, and C occupies 8 bytes ---------------------------------
Class or object length: Non-virtual functions are equivalent to the global ones, not in the class. Static is global, not in the class. However, the const needs to allocate space.

Non-static variables, virtual function linked list (if there is a virtual function in the class) --------- allocate space

Original article: http://www.cppblog.com/mzty/archive/2005/10/24/832.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.