Sizeof function Summary

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_5c717fa001012ml7.html

Http://baike.baidu.com/view/1078660.htm

Sizeof function: calculate the number of bytes in the Data Space

1. Comparison with strlen ()
Strlen calculates the number of characters in the character array. It ends with "\ 0" and does not count as an array element of '\ 0.
Sizeof memory space occupied by computing data (including arrays, variables, types, struct, etc.), expressed by the number of nodes (of course, used in the character array to calculate the size of "\ 0 ).
2. sizeof operations for pointers and static Arrays
Pointers can be viewed as one type of variables. The sizeof operation result for all pointer variables is 4.

Example 1: char * P;
Sizeof (p) = 4;
Sizeof (* P) = 1; // Equivalent to sizeof (char ); 
Instance 2: 
For static arrays, sizeof can directly calculate the array size;
Example: int A [10];
Char B [] = "hello ";
Sizeof (a) equals 4*10 = 40;
Sizeof (B) is 6;

VoidFun (char P [])
{
 Sizeof (P );       // Equal to 4. When an array is used as a type parameter, the array name is used as a pointer !!
}

Example 3 (classic question ):
Double * (* A) [3] [6];
Cout <sizeof (a) <Endl; // 4 A is a pointer
Cout <sizeof (* A) <Endl; // 72 * A is an array with 3*6 pointer Elements
Cout <sizeof (** A) <Endl; // 24 ** A is the Six pointers of the array in one dimension.
Cout <sizeof (*** A) <Endl; // 4 *** A is the first pointer of one dimension.
Cout <sizeof (***** A) <Endl; // 8 ***** A is a double variable.
Problem Analysis:
A is a very strange definition, which indicates a pointer to an array of the double * [3] [6] type. Since it is a pointer, sizeof (a) is 4.
Since a is a pointer of the double * [3] [6] type, * a indicates a multi-dimensional array of the double * [3] [6] type. Therefore, sizeof (*) = 3*6 * sizeof (double *) = 72.
Similarly, ** A indicates an array of the double * [6] type. sizeof (** A) = 6 * sizeof(Double *) = 24.
* ** A indicates an element, that is, double *. Therefore, sizeof (*** A) = 4.
* *** A is a double, so sizeof (*** A) = sizeof (double) = 8.

3. Format
Sizeof operator. variables or objects can be left with no brackets. But if it is a type, parentheses must be added.
4. Notes about string when using sizeof
String S = "hello ";
Sizeof (s) is equal to the size of the string class (32), sizeof (S. c_str () is obtained with the string length (4 ).
5. Space Calculation of union and struct
The two principles are followed in general:
(1) The total space is an integer multiple of the bytes occupied by the largest member (type ).
(2) Data Alignment principle-data is sorted by structural members in the memory. When this member variable is exceeded, the size of the Space placed before it must be an integer multiple of the member type. If not, fill in the space, and so on .....
Note: arrays are placed one by one based on a single variable, rather than as a whole. If the member has a custom class or struct, you should also pay attention to the array problem.
Example 4: [reference content of other posts]
The sizeof structure is complicated due to alignment. See the following example: (the default alignment Mode)
Struct S1
{
 Char;
 Double B;
 Int C;
 Char D;
};

Struct S2
{
 Char;
 Char B;
 Int C;
 Double D;
};

Cout <sizeof (S1) <Endl; // 24
Cout <sizeof (S2) <Endl; // 16

It is also two Char Types, one int type and one double type, but their sizes are different due to alignment problems. The element pendulum method can be used to calculate the struct size. For example, the CPU determines the peer bounds of the struct. According to the conclusion in the previous section, both S1 and S2 have the largest element type, that is, double type's ter8. Then, each element is placed.
For S1, first place a to the peer interface of 8, assuming that it is 0. At this time, the next idle address is 1, but the next element B is of the double type, the closest address to 1 is 8, so B is placed in 8. At this time, the next idle address becomes 16, and the peer interface of the next element C is 4 or 16, therefore, C is placed at 16. At this time, the next idle address is changed to 20. The next element D needs to be bound to 1, which also falls in the opposite world. Therefore, D is placed on 20, the struct ends at address 21. Because the size of S1 must be a multiple of 8, the space from 21-23 is retained, and the size of S1 is changed to 24.
For S2, first place a to the peer interface of 8, assuming that it is 0, then the next idle address is 1, and the peer interface of the next element is also 1, so B is placed in 1, the next idle address is changed to 2; the peer interface of the next element C is 4, so take the address closest to 2 4 and place it in C. The next idle address is changed to 8, the peer interface of the next Element D is 8, so d is placed in 8. After all the elements are placed, the struct structure ends at 15 points, occupying a total space of 16, which is exactly a multiple of 8.

There is a trap here. For struct members in the struct, do not consider its alignment as its size. See the following example:
Instance 5:
Struct S1
{
 Char A [8];
};

Struct S2
{
 Double D;
};

Struct S3
{
 S1 S;
 Char;
};

Struct S4
{
 S2 S;
 Char;
};
Cout <sizeof (S1) <Endl; // 8
Cout <sizeof (S2) <Endl; // 8
Cout <sizeof (S3) <Endl; // 9
Cout <sizeof (S4) <Endl; // 16;
The size of S1 and S2 is 8, but the alignment of S1 is 1 and S2 is 8 (double). Therefore, this difference exists in S3 and S4.
Therefore, when you define a struct, if the space is insufficient, consider alignment to arrange the elements in the struct.

  Correlation constant:
  Sizeof INT: 4
  Sizeof short: 2
  Sizeof long: 4
  Sizeof float: 4
  Sizeof double: 8
  Sizeof CHAR: 1
  Sizeof P: 4
  Sizeof word: 2
  Sizeof DWORD: 4

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.