Fundamentals of C + + programming (5) Use of sizeof

Source: Internet
Author: User
Tags array length int size

1. Knowledge points

(1) sizeof is a single-head operation, not a function that gets the number of bytes in the memory space that the operand occupies.

(2) The operand of sizeof can make the type name, or it can be an expression, if the type name is directly obtained the number of bytes of the type, if it is an expression, the type of the result of the expression is parsed first, and then according to the type to determine the number of bytes, do not actually calculate the expression.

1 int 1 ; 2 Double 1.5 ; 3 sizeof (int); // result is 4 4 sizeof (a);    // result is 4 5 sizeof (b);    // result is 8

(3) sizeof is seldom used alone, but is used in conjunction with requirements such as memory allocation or the array length of the calculation method.

1 // use with memory space allocation 2 int *ptr = (int *)malloc(sizeof(int); 3 // use with calculated array lengths 4 int sizeof sizeof (double);

(4) When the array name is the operand, the entire array occupies space, and when the array name is passed to the child function as an argument, the array names become pointers, and the result will be the space occupied by the pointer.

voidSubfunc (Doubledarray[]) {cout<<sizeof(Darray)/sizeof(Double) << Endl;//Output 4/8=0}intMainintargcChar*argv[]) {    Doubledarray[ -]; cout<<sizeof(Darray)/sizeof(Double) << Endl;//Output 16/8=20Subfunc (Darray);    GetChar (); return 0;}

(5) Use strlen when calculating the length of a string. sizeof is an operator used to get the memory size of a type or expression; Strlen is a function that calculates the number of characters in a string, where the string endings are not counted.

2. Face question 2.1 expression that cannot be calculated using sizeof
1 structBaby2 {3UnsignedintGender:1;4UnsignedintWeight:5;5UnsignedintWeek:7;6UnsignedintBlood:3;7UnsignedintHeight:8;8 };9 intTripleintNumber ) {Ten     return 3*Number ; One } A intMainintargcChar*argv[]) { -     sizeof(baby);//A -     sizeof(Baby.gender);//B the     sizeof(triple);//C -     sizeof(Triple (3));//D -     sizeof(Show ());//E -  + GetChar (); -     return 0; +}

Answer: Correct: A, D; Error: B,c,e.

(1) sizeof computational structure is the result of the structure of the occupied memory space, the structure of the use of a bit domain definition members, require a bit domain member can not span two bytes, it can be calculated that the structure of the number of bytes is 4,a correct.

(2) The result of sizeof's calculation is the number of bytes, but it is not allowed to compute the space occupied by a member of a bit field in the struct, although it is possible to calculate if the bit field is not defined. So B is wrong.

(3) does not allow the calculation of the function name of the size of the number of bytes, a bit inexplicable, C wrong.

(4) The expression is allowed to be evaluated, and the result is the number of bytes of the type of the function return value, that is, the int size of the problem, and D is correct.

(5) The function return value type is indeterminate, does not allow to calculate by sizeof, therefore e error.

2.2SIZEOF computational structure is the memory for its problem

The results of sizeof calculation for the following two structures are written

struct s1{    char  a;//1    short  b;//4    int  C;//8     Double  d;//16,16 is just an integer multiple of double size}; struct S2 {    char  a;//1    short  b;//4    Double  C;//16 (starting from 9)    int  D;//20 (the result is not an integer multiple of double, it needs to be added to)};

The data summarizes it: (1) Each member must have an integer multiple of its own size in memory.

(2) The result of the final structure calculation must be an integer multiple of the member with the largest number of bytes.

Answer: 16, 24.

2.3 Calculation of sizeof when the structure is nested

1 structS1 {2     intA;3 };4 structS2 {5     Chara[4];6 };7 structs3{8     Chara[4];9     Charb;Ten }; One structS4 { A S1 A; -     Charb; - }; the structs5{ - S2 A; -     Charb; -};

The data for the complex structure is summarized as follows: In the case of data, the underlying data types in the structure are the most fundamental. For example, if a struct member is an array, the type of the element in the array should be the standard for the data, and if the struct member is a different struct, it should be the standard for the outer structure's data alignment as a member of the underlying element type in the inner structure body. So the answer is as follows

 1  sizeof  ( struct  S1) =4   2  sizeof  ( struct  s2) =4  ;  3  sizeof  ( struct  S3) =5  ;  4  sizeof  ( struct  S4) =8  ;  5  sizeof  ( struct  S5) =5 ; 

C + + Programming Fundamentals (5) use of sizeof

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.