Sizeof () usage Summary

Source: Internet
Author: User
Sizeof () function: calculate the number of bytes in the Data Space
 

1. Comparison with strlen ()
Strlen () is used to calculate the number of characters in the character array. It ends with "\ 0" and does not count as an array element of '\ 0.
The memory space occupied by sizeof computing data (including arrays, variables, types, struct, etc.) is expressed by the number of nodes.
 
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.
Note: int * P; sizeof (p) = 4; but sizeof (* P) is equivalent to sizeof (INT );
 
For static arrays, sizeof can directly calculate the array size;
For example, int A [10]; char B [] = "hello ";
Sizeof (a) equals 4*10 = 40;
Sizeof (B) is 6;
Note: When an array is used as a type parameter, the array name is used as a pointer !!
Void fun (char P [])
{Sizeof (p) equals 4}

Classic questions:
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 resolves a problem with a double variable: A is a very strange definition, it 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, so sizeof (** A) = 6 * sizeof (double *) = 24. * ** A indicates an element, that is, double *. Therefore, sizeof (*** A) = 4. As for *** A, it 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. sizeof (S. c_str () returns the length of the string.

5. Space Calculation of union and struct
The two principles are followed in general:
(1) The overall space is an integral multiple of the number of bytes occupied by the largest member (type ).
(2) Data Alignment principle-memory is arranged in the order of structure members. When this member variable is exceeded, the size of the Space placed before it must be an integer multiple of the size of the member type, if it is not enough, complete it, 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.

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

More: http://www.cnblogs.com/chengxin1982/archive/2009/01/13/1374575.html

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.