About the key words sizeof and its ten characteristics

Source: Internet
Author: User

     sizeof is a keyword in c/s + + that is an operator that takes the length of an object (data type or data object) (that is, the size of the memory, in bytes). Types include basic data types (excluding void), user-defined types (struct, class), and function types. A data object is an ordinary variable and a pointer variable (containing a void pointer) defined with the previously mentioned type. Top Ten Features: attribute 0:sizeof is an operator, not a function; the attribute 1:sizeof cannot find the length of the void type; the attribute 2:sizeof the length of the pointer of void type;    actually the pointer is also a variable, but this variable is very special , which is the variable that holds the address of other variables, the size of the pointer variable under the 32-bit computer platform is 4 characteristic 3:sizeof can obtain the length of the array of statically allocated memory; Note: Use sizeof to find the length of the string to add the end of the '/0 ' example: void fun (int &NBSP;ARRAY[10]) {int n = sizeof (array);} Editing program: #include <iostream>using namespace std; int fun (int array[10]) {int n  = sizeof (array); return n;} Int main () {int ret;int array[10] = { 0 };ret = fun (array); cout  <<ret<< endl;system ("pause"); return 0;} Run Result: 4 Press any key to continue . . .     the value of the Fun class n is 4, not 40, because when the function argument is passed, the array is converted to a pointer, and if the entire array is passed directly, Then there must be a copy of the array element (the actual parameter to the copy of the parameter), when the array is very large, it will result in very inefficient function execution, but only the address of the array (that is, the pointer), only need to copy 4byte. The 4:sizeof attribute cannot find the size of the dynamically allocated memory, and the attribute 5:sizeof cannot find the length of an incomplete array.Otherwise, a compile error occurs; attribute 6: When the expression is the operand of sizeof, it returns the type size of the result of the expression evaluation, but it does not evaluate the expression; Example: Program: #include <iostream>using namespace  std;int main () {char ch = 1;int num = 1;int n1 = sizeof (Ch + num); int n2 = sizeof (ch=ch + num);cout << "n1=" <<n1 << endl;cout << "n2="  << n2 << endl;system ("pause"); return 0;} Result: N1=4n2=1 Press any key to continue . . .     because of the default type conversion, the expression Ch + num evaluates to an int, so n1= 4; The ch=ch + num evaluates to char, although the result of the calculation of ch + num is int, when the result is assigned to CH and then the type conversion, so the final type of expression is char, so n2=1. The 7:sizeof attribute can be sized for a function call, and the size obtained is equal to the size of the return type (function type), but does not execute the function body; the size of the struct (and its objects) evaluated by the attribute 8:sizeof is not equal to the size of each data member object. ; rules: A. The size of the struct is equal to the integer multiple of the maximum member size in the structure B. The first address of a member of a struct is an integer multiple of its type relative to the first address of the struct, such as the address offset of a double member relative to the first address of the struct should be a multiple of 8 C. To satisfy rules 1 and 2, After the byte fill example: Program: #include <iostream>using namespace std;int main () {STRUCT&NBSP;A{INT&NBSP;NUM1 =0;int num2=0;double num3=0;};  struct B{int n1=0;double n2=0;int n3=0;}; cout << "A=" <<sizeof (A) << endl;cout <<  "b=" << sizeof (B) << endl;system ("pause"); return 0;} Result: a=16b=24 Press any key to continue.  . .sizeof (A): 4+4+8=16sizeof (B): 4 (N1 occupy address space: 0,1,2,3) +4 (not enough 8 multiples to fill 4 address space, 4,5,6,7) + 8 (N2 occupied address space: 8-15) +4 (N3 occupied address space: 16-19) +4 (not enough 8 multiples of 4, 20,21,22,23) = 24. The attribute 9:sizeof cannot be used to calculate the size of the bit-field members of a struct, but the size of the struct that contains the bit-field members can be obtained.


This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1760133

About the key words sizeof and its ten characteristics

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.