# sizeof () Usage Summary

Source: Internet
Author: User

sizeof () function: Calculates the number of bytes in a data space
1. Comparison with strlen ()
Strlen () computes the number of characters in an array of characters, with "a" as the end, and does not calculate the array element as ' \ s '.
sizeof calculates the amount of memory space that the data (including arrays, variables, types, structures, and so on) occupies, expressed in bytes.
2. sizeof operation of pointers and static arrays
Pointers 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 the equivalent of sizeof (int);
For static arrays, sizeof can calculate the array size directly;
Example: int a[10];char b[]= "Hello";
sizeof (a) equals 4*10=40;
sizeof (b) equals 6;
Note: array names are used as pointers when arrays are made of parameters!!
void Fun (char p[])
{sizeof (P) equals 4} Classic Questions:
double* (*a) [3][6];
cout<<sizeof (a) <<endl;//4 a as pointer
cout<<sizeof (*a) <<endl;//72  *a is an array of 3*6 pointer elements
cout<<sizeof (* * A) <<endl; 24 **a 6 pointers to array one-dimensional
cout<<sizeof (* * *a) <<endl; 4 ***a is the first pointer of one dimension
cout<<sizeof (****a) <<endl; 8 ****a as a double variable Problem Resolution:ais a very strange definition, he represents a pointer to an array of type double*[3][6]. Since it is a pointer, sizeof (a) is 4.
since a is a pointer to a double*[3][6] type,*a represents a multidimensional array type of double*[3][6], so sizeof (*A) =3*6*sizeof (double*) =72**a< Span lang= "en-US" >double*[6] sizeof (**A) =6*sizeof  (double*) =24***adouble* so < Span lang= "en-US" >sizeof (***a) =4****a< Span lang= "en-US" >double so sizeof (****a ) =sizeof (double) =8 .
3. Format notation
sizeof operator, the variable or object can be without parentheses, but if the type, you need to add parentheses.
4Considerations for string when using sizeof
String s= "Hello";
sizeof (s) equals the size of the string class, and sizeof (S.C_STR ()) Gets the length of the string.
Spatial calculation of 5.union and struct
In general, two principles are followed:
(1) The overall space isThe 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 discharged, the space size before it must be an integral multiple of the size of the member type, and if not enough, the backward analogy ....
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 complicated, see the following example:( default alignment )
struct S1
{
Char A;
Double b;
int C;
Char D;
};

struct S2
{
Char A;
Char b;
int C;
Double D;
};

Cout<<sizeof (S1) <<endl; 24
cout<<sizeof (S2) <<endl; 16

also two Charint Type, a double type, but because align issues, causing them to vary in size. The size of the structure can be calculated using the element placement method, I give an example: First, cpu Judging the bounds of the structure, according to the conclusions of the previous section, s1s2double< Span lang= "en-US" >8
ForS1, first putAPut in8Of the bounded, assuming that the0, at which point the next idle address is1, but the next elementDIsDoubleType that you want to place in8On the boundary, away from1The closest address is8Up, soDwas put on the8, the next idle address becomes16, the next elementCThe right to the boundary is4，16Can be met, soCPut it in the16, the next idle address becomes20, the next elementd need to be bounded 1d 2021s1 the size needs to be < Span lang= a multiple of "en-us" >821-23 space is reserved, s1 size becomes 24
ForS2, first putAPut in8Of the bounded, assuming that the0, the next idle address is1, the next element of the bounds is also1SoBPlaced in1, the next idle address becomes2; next elementCThe right to the boundary is4, so take away2Nearest address4 placed c8d8 d placed in 8 15 end, occupying total space of 168

Here's a trap, for struct members in a struct, don't think its alignment is his size, look at the following example:
struct S1
{
Char A[8];
};

struct S2
{
Double D;
};

struct S3
{
S1 s;
Char A;
};

struct S4
{
S2 s;
Char A;
};
Cout<<sizeof (S1) <<endl; 8
cout<<sizeof (S2) <<endl; 8
Cout<<sizeof (S3) <<endl; 9
Cout<<sizeof (S4) <<endl; 16;
S1 and s2 8s11,s2 8 (double 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:

struct S1
{
int i:8;
int j:4;
Double b;
int a:3;
};

struct S2
{
int i;
Int J;
Double b;
int A;
};

struct S3
{
int i;
Int J;
int A;
Double b;
};

struct S4
{
int i:8;
int j:4;
int a:3;
Double b;
};

Cout<<sizeof (S1) <<endl; 24
cout<<sizeof (S2) <<endl; 24
Cout<<sizeof (S3) <<endl; 24
Cout<<sizeof (S4) <<endl; 16
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: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

sizeof () Usage Summary

Related Keywords:

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.