sizeof () Usage Summary

Source: Internet
Author: User

sizeof() Function: Calculates the number of bytes in a data space1. Compare strlen () with strlen () to calculate the number of characters in the character array to" /"To end the judgment, do not count as' /'the array element. sizeof calculates the amount of memory space that the data (including arrays, variables, types, structures, and so on) occupies, expressed in bytes. 2The sizeof manipulation pointers for pointers and static arrays can be seen as one of the variable types. All pointer variables have a sizeof operation result of 4. Note:int*p;sizeof(p) =4; But sizeof (*P) is equivalent to sizeof (int); For a static array, sizeof can calculate the array size directly, for example:inta[Ten];Charb[]="Hello"; sizeof(a) equals to theTen= +; sizeof(b) equals 6; Note: Array names are used as pointers when arrays are made of parameters!! voidFunCharp[]) {sizeof(P) equals 4} Classic question:Double* (*A) [3][6]; cout<<sizeof(a) <<endl;//4 A is a pointercout<<sizeof(*a) <<endl;//The *a is an array of pointer elements with 3*6cout<<sizeof(**a) <<endl;//**a 6 pointers for one-dimensional arrayscout<<sizeof(***a) <<endl;//4 ***a is the first pointer of one dimensioncout<<sizeof(****a) <<endl;//8 ****a is a double variableProblem Resolution: A is a very strange definition, he says a point to double*[3][6] Pointer to an array of type.       Since it is a pointer, sizeof (a) is 4. Since a is the execution double*[3][6] Type of pointer, *a represents a double*[3][6] is a multidimensional array type, so sizeof (*a) =3*6*sizeof(Double*)= the。 Similarly, **a represents a double*[6] Type of array, so sizeof (**a) =6*sizeof(Double*)= -。 A means one of the elements, namely double*, so sizeof (***a) =4。 As for ****a, it is a double, so sizeof (****a) =sizeof(Double)=8. 3The . Format of the sizeof operator, the variable or object can be without parentheses, but if the type, the parentheses are required. 4Considerations for string when using sizeofstrings="Hello"; sizeof(s) equals the size of the string class,sizeof(S.c_str ()) is obtained with the string length. 5the spatial computation of union and Struct follows two principles in general: (1The total space is the integer multiple of the number of bytes occupied by the member (the type) that occupies the largest space (2) Data Alignment principle----The memory is arranged in the order of the structure members, and when the member variable is queued, the amount of space placed before it must be an integer multiple of the size of the member type, if not enough, to be done backwards and forwards .... Note: Arrays are placed in a single variable rather than as a whole. If you have a custom class or struct in a member, you should also be aware of array problems. Example: [referring to the content of other posts] because the alignment problem makes the structure of sizeof more complex, see the following example: (Default alignment)structs1{CharA;Doubleb;intC;CharD;};structs2{CharA;Charb;intC;DoubleD;}; cout<<sizeof(S1) <<endl;// -cout<<sizeof(S2) <<endl;// -The same is two char type, an int type, a double type, but because of the alignment problem, their size is different. The size of the structure can be calculated using the element placement method, I give an example: First, the CPU to determine the structure of the bounds, according to the previous section, the S1 and S2 to the bounds of the largest element type, that is, the double type of the bounds 8.  Then start placing each element. For S1, first put a to 8 of the bounds, the assumption is 0, at this time the next idle address is 1, but the next element D is a double type, to put to 8 of the bounds, the nearest address 1 is 8, so D is placed in 8, at this time the next idle address becomes 16, the next element c of the bounds is 4, 16 can be satisfied, so C is placed in 16, at this time the next idle address becomes 20, the next element D needs to the bounds 1, also just falls on the bounds, so D is placed in 20, the structure ends at address 21. Since the size of the S1 needs to be a multiple of 8,-23 of the space is reserved, and the size of the S1 becomes 24. For S2, first put a to 8 of the bounds, the assumption is 0, at this time the next idle address is 1, the next element of the bounds is also 1, so B is placed in 1, the next idle address becomes 2, the next element c is 4, so take the 2 nearest address 4 is placed C, the next idle address becomes 8,  The next element d of the bounds is 8, so D is placed in 8, all elements are placed, the structure ends at 15, occupying a total space of 16, just a multiple of 8. Here's a trap, for struct members in a struct, don't think its alignment is his size, look at the following example:structs1{Chara[8];};structs2{DoubleD;};structs3{s1 S;CharA;};structs4{s2 S;CharA;}; cout<<sizeof(S1) <<endl;//8cout<<sizeof(S2) <<endl;//8cout<<sizeof(S3) <<endl;//9cout<<sizeof(S4) <<endl;//;The S1 and S2 sizes are 8, but the S1 alignment is 1,s2 is 8 (Double), so there is such a difference in S3 and S4.   Therefore, when you define a structure, if space is tense, it is best to consider the alignment factors to arrange the elements in the structure. Add: Do not let double interfere with your bit field in structs and classes, you can use bit fields to specify the space that a member can occupy, so using bit fields can save a certain amount of space that the structure occupies. But consider the following code:structS1 {intI8; intJ:4; Doubleb;intA:3; }; structS2 {inti;intJ;Doubleb;intA;}; structS3 {inti;intJ;intA;Doubleb;}; structS4 {intI8; intJ:4; intA:3; Doubleb;}; cout<<sizeof(S1) <<endl;// -cout<<sizeof(S2) <<endl;// -cout<<sizeof(S3) <<endl;// -cout<<sizeof(S4) <<endl;// -As you can see, there is a double presence that interferes with the in-place domain (sizeof's algorithm references the previous section), so when using bit fields, it is best to place the float type and the double type at the beginning or last of the program. Related constants:sizeof int:4sizeof  Short:2sizeof Long:4sizeof float:4sizeof Double:8sizeof Char:1sizeofP:4sizeofWord:2sizeofDWORD:4 

sizeof () Usage Summary

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.