Sizeof Calculation Method

Source: Internet
Author: User

CPP Code
// Example of the sizeof keyword
Size_t I = sizeof (INT );

Struct align_depends {
Char C;
Int I;
};
Size_t size = sizeof (align_depends); // The value of size depends on
// The value set with/ZP or
// # Pragma pack

Int array [] = {1, 2, 3, 4, 5}; // sizeof (array) is 20
// Sizeof (array [0]) is 4
Size_t sizearr = // count of items in array
Sizeof (array)/sizeof (array [0]);
// Example of the sizeof keyword
Size_t I = sizeof (INT );

Struct align_depends {
Char C;
Int I;
};
Size_t size = sizeof (align_depends); // The value of size depends on
// The value set with/ZP or
// # Pragma pack

Int array [] = {1, 2, 3, 4, 5}; // sizeof (array) is 20

// Sizeof (array [0]) is 4
Size_t sizearr = // count of items in array
Sizeof (array)/sizeof (array [0]);
<SCRIPT src = "MS-its: dsmsdn. chm:/html/msdn_footer.js"> </SCRIPT>

1. Usage
1.1 like new and delete, sizeof is a keyword, not a function or macro.
1.2 sizeof returns the number of bytes allocated in the memory, which is related to the number of bits in the operating system. For example, in a common 32-bit system, the int type occupies 4 bytes, but in a 16-bit system, the int type occupies 2 bytes.
1.3 The sizeof parameter can be a type, a variable, or a constant. For the same type, the sizeof returned values of the form parameters in the preceding three are the same.
CPP Code
Int;
Sizeof (a); // = 4
Sizeof (INT); // = 4
Sizeof (1); // = 4
Int;
Sizeof (a); // = 4
Sizeof (INT); // = 4
Sizeof (1); // = 4


1.4 c99 standard stipulates that functions, expressions of undetermined types, and bit-field members cannot be computed in S
Izeof value, that is, the following statements are incorrect.
CPP Code
Void FN (){}
Sizeof (FN); // error: Function
Sizeof (FN (); // error: the type cannot be determined.
Struct s
{
Int A: 3;
};
S sa;
Sizeof (SA. a); // error: Bit Domain Member
Void FN (){}
Sizeof (FN); // error: Function
Sizeof (FN (); // error: the type cannot be determined.
Struct s
{
Int A: 3;
};
S sa;
Sizeof (SA. a); // error: Bit Domain Member

1.5 sizeof is processed in the compilation phase. Because sizeof cannot be compiled into machine code, sizeof parameters cannot be compiled but replaced with types.
CPP Code
Int A =-1;
Sizeof (A = 3); // = sizeof (A) = sizeof (INT) = 4
Cout <A <Endl; // output-1. Because the "=" operator returns the type of the left operand, the value assignment operation is not executed.
Int A =-1;
Sizeof (A = 3); // = sizeof (A) = sizeof (INT) = 4
Cout <A <Endl; // output-1. Because the "=" operator returns the type of the left operand, the value assignment operation is not executed.


2. Different types of memory allocation in 32-bit Systems
2.1 Basic Types
CPP Code
Sizeof (INT); // = 4
Sizeof (double); // = 8
Sizeof (char); // = 1
Sizeof (bool); // = 1
Sizeof (short); // = 2
Sizeof (float); // = 4
Sizeof (long); // = 4
Sizeof (INT); // = 4
Sizeof (double); // = 8
Sizeof (char); // = 1
Sizeof (bool); // = 1
Sizeof (short); // = 2
Sizeof (float); // = 4
Sizeof (long); // = 4

2.2 pointer
The pointer occupies 4 bytes in a 32-bit system.
CPP Code
Sizeof (int *); // = 4
Sizeof (double *); // = 4
Sizeof (char *); // = 4
Sizeof (int *); // = 4
Sizeof (double *); // = 4
Sizeof (char *); // = 4

Array 2.3
2.3.1 the sizeof of array returns the number of bytes occupied by the entire array (number of array elements × bytes occupied by each element ).
CPP Code
Int Ai [] = {1, 2 };
Sizeof (AI); // = 2*4 = 8
Int Ai [] = {1, 2 };
Sizeof (AI); // = 2*4 = 8


2.3.2 The Memory Allocation Method for constant strings and character arrays is the same.
CPP Code
Char AC [] = "ABCD"; // note the string Terminator '\ 0' at the end of the array'
Sizeof (AC); // = 5*1 = 5
Sizeof ("ABCD"); // = 5*1 = 5
Char AC [] = "ABCD"; // note the string Terminator '\ 0' at the end of the array'
Sizeof (AC); // = 5*1 = 5
Sizeof ("ABCD"); // = 5*1 = 5


2.3.3 the number of bytes occupied by arrays and pointers is different.
CPP Code
Int * Pi = new int [10]; // This is a pointer
Sizeof (PI); // = 4

Int Ai [10];
Int * P = AI; // This is a pointer.
Sizeof (p); // = 4

Double * (* A) [3] [6]; // It is treated as a (double *) (* A) [3] [6], which is a 3 × 6 two-dimensional array, the array element is a pointer and points to the double type.

Sizeof (a); // = 4, A is a pointer to the preceding two-dimensional array
Sizeof (* A); // = sizeof (double *) * 3*6 = 72, * a represents the preceding two-dimensional array
Sizeof (** A); // = sizeof (double *) * 6 = 24, ** A is * (* A), which indicates double * [6], is a one-dimensional array with the element as the double pointer.

Sizeof (*** A); // = sizeof (double *) = 4, indicating the first element in the preceding one-dimensional array. The element type is a double pointer.

Sizeof (*** A); // = sizeof (double) = 8, which indicates the double type to which the first element of the preceding array points.
Int * Pi = new int [10]; // This is a pointer
Sizeof (PI); // = 4

Int Ai [10];
Int * P = AI; // This is a pointer.
Sizeof (p); // = 4

Double * (* A) [3] [6]; // It is treated as a (double *) (* A) [3] [6], which is a 3 × 6 two-dimensional array, the array element is a pointer and points to the double type.
Sizeof (a); // = 4, A is a pointer to the preceding two-dimensional array
Sizeof (* A); // = sizeof (double *) * 3*6 = 72, * a represents the preceding two-dimensional array
Sizeof (** A); // = sizeof (double *) * 6 = 24, ** A is * (* A), which indicates double * [6], is a one-dimensional array with the element as the double pointer.
Sizeof (*** A); // = sizeof (double *) = 4, indicating the first element in the preceding one-dimensional array. The element type is a double pointer.
Sizeof (*** A); // = sizeof (double) = 8, which indicates the double type to which the first element of the preceding array points.

2.3.4 The array in the function form parameter will be changed to a pointer because the array parameter is "called by address". The caller only needs to pass the address of the real parameter. One exception is that the parameter is a pointer to an array.
CPP Code
Void ACF (char P [3]) // The parameter type is int [], indicating the pointer to the int
{
Sizeof (p); // = 4
}
Void AIF (int p []) // The parameter type is int [], indicating the pointer to the int
{
Sizeof (p); // = 4
}
Void PIF (INT (* P) [6]) // The parameter type is int (*) [6], which indicates the pointer to the int array.
{
Sizeof (p); // = 4
Sizeof (* P); // = sizeof (INT) * 6 = 24
}
Void PPF (int * P [6]) // The parameter type is int * [], which indicates the pointer to the int pointer.
{
Sizeof (p); // = 4
Sizeof (* P); // = 4
}
Void ACF (char P [3]) // The parameter type is int [], indicating the pointer to the int
{
Sizeof (p); // = 4
}
Void AIF (int p []) // The parameter type is int [], indicating the pointer to the int
{
Sizeof (p); // = 4
}
Void PIF (INT (* P) [6]) // The parameter type is int (*) [6], which indicates the pointer to the int array.
{
Sizeof (p); // = 4
Sizeof (* P); // = sizeof (INT) * 6 = 24
}
Void PPF (int * P [6]) // The parameter type is int * [], which indicates the pointer to the int pointer.
{
Sizeof (p); // = 4
Sizeof (* P); // = 4


2.4. Class and struct memory allocation.
2.4.1 empty class or empty struct occupies one byte.
Class cempty {};
Sizeof (cempty); // = 1

Struct sempty {};
Sizeof (sempty); // = 1
Class cempty {};
Sizeof (cempty); // = 1

Struct sempty {};
Sizeof (sempty); // = 1
2.4.2 non-empty classes and struct occupy the sum of all Members, but do not include the space occupied by member functions and static members.
CPP Code
Class CINT: Public cempty {
Int I;
};
Sizeof (CINT); // = 4;

Class cfunc {
Void F (){}
};
Sizeof (cfunc); // = 1;

Struct Sint: sempty {
Static int I;
};
Sizeof (Sint); // = 1;
Class CINT: Public cempty {
Int I;
};
Sizeof (CINT); // = 4;

Class cfunc {
Void F (){}
};
Sizeof (cfunc); // = 1;

Struct Sint: sempty {
Static int I;
};
Sizeof (Sint); // = 1;

2.4.3 byte alignment
To speed up computer data acquisition, the compiler performs byte alignment on memory by default. The principle of byte alignment for struct (including classes) is:
1) The first address of the struct variable can be divisible by the size of its widest basic type member;

2) The offset (offset) of each member of the struct to the first address of the struct is an integer multiple of the member size. If necessary, the compiler will add the internal adding between the members );

3) the total size of the struct is an integer multiple of the size of the widest basic type of the struct. If necessary, the compiler will add the trailing padding after the last member ).

CPP Code
Struct sbyte1
{
Double D; // offset 0 ~ 7
Char J; // offset 8
Int A; // offset 12 ~ 15. Because 9 cannot be divided into 4, first fill 9 ~ 11
};
Sizeof (sbyte1); // = 16

Struct sbyte2
{
Char J; // offset 0
Double D; // offset 8 ~ 15. Because 1 cannot divide 8, first fill 1 ~ 7
Int A; // offset 16 ~ 19
};
Sizeof (sbyte2); // = 24. Fill in 20 ~ 23
Struct sbyte1
{
Double D; // offset 0 ~ 7
Char J; // offset 8
Int A; // offset 12 ~ 15. Because 9 cannot be divided into 4, first fill 9 ~ 11
};
Sizeof (sbyte1); // = 16

Struct sbyte2
{
Char J; // offset 0
Double D; // offset 8 ~ 15. Because 1 cannot divide 8, first fill 1 ~ 7
Int A; // offset 16 ~ 19
};
Sizeof (sbyte2); // = 24. Fill in 20 ~ 23

In addition, you can use # pragma pack (n) to set the variable to n-byte alignment.
CPP Code
# Pragma pack (push) // save alignment status
# Pragma pack (4) // set to 4-byte alignment
Class cbyte
{
Char C; // offset 0
Double D; // offset 4 ~ 11. Because 1 cannot be divided into 4, first fill 1 ~ 3
Int I; // offset 12 ~ 15
};
# Pragma pack (POP) // restore alignment
Sizeof (cbyte); // = 16
# Pragma pack (push) // save alignment status
# Pragma pack (4) // set to 4-byte alignment
Class cbyte
{
Char C; // offset 0
Double D; // offset 4 ~ 11. Because 1 cannot be divided into 4, first fill 1 ~ 3
Int I; // offset 12 ~ 15
};
# Pragma pack (POP) // restore alignment
Sizeof (cbyte); // = 16


2.4.4-bit domain
When storing some information, it does not need to occupy a full byte, but only needs to occupy a few or one binary bit. For example, when storing a switch value, there are only two States: 0 and 1. Use one binary digit. To save storage space and simplify processing, the C language also provides a data structure called "bit domain" or "bit segment ". The so-called "bit field" refers to dividing the binary character in a byte into several different regions and showing the digits of each region.


2.4.4.1 bit fields are measured in bits. The length cannot exceed one byte. A single-byte domain must be stored in the same byte. If the remaining space of one byte is insufficient to store another domain, it should be stored in the next unit.

CPP Code
Struct sbit1
{

Char A: 3;
Char B: 4;
Char C: 5;
};
Sizeof (sbit1); // = (3 + 4 + 1 + 5 + 3) bits = 2 bytes
Struct sbit1
{

Char A: 3;
Char B: 4;
Char C: 5;
};
Sizeof (sbit1); // = (3 + 4 + 1 + 5 + 3) bits = 2 bytes
Sbit1: | A × 3 + B × 4 + # × 1 | c × 5 + # × 3 |

2.4.4.2 The use of airspace may intentionally start a domain from the next unit, but the airspace cannot be used.

CPP Code
Struct sbit2
{
Char A: 3;
CHAR: 0; // airspace
Char B: 4;
Char C: 5;
};
Sizeof (sbit2); // = (3 + 4 + 1 + 5 + 3) bits = 3 bytes
Struct sbit2
{
Char A: 3;
CHAR: 0; // airspace
Char B: 4;
Char C: 5;
};
Sizeof (sbit2); // = (3 + 4 + 1 + 5 + 3) bits = 3 bytes
Sbit2: | A × 3 + # × 5 | B × 4 + # × 4 | c × 5 + # × 3 |

2.4.4.3 if the types of adjacent bit field are different, the implementations of each compiler are different. vc6 adopts the non-compression mode and Dev-C ++ adopts the compression mode.
CPP Code
Struct sbit3
{
Char A: 3;
Short B: 4;
Char C: 5;
};
Sizeof (sbit3); // = 6 bytes. Because of the different adjacent bit domain types, in vc6, sizeof is 6 and Dev-C ++ is 2.
Struct sbit3
{
Char A: 3;
Short B: 4;
Char C: 5;
};
Sizeof (sbit3); // = 6 bytes. Because of the different adjacent bit domain types, in vc6, sizeof is 6 and Dev-C ++ is 2.
Sbit3 (not compressed): | A × 3 | # × 8 | B × 4 + # × 4 | # × 8 | c × 5 + # × 3 | # × 8 |


Sbit3 (compression): | a x 3 + B x 4 + # x 1 | C x 5 + # x 3 |

2.4.4.4 do not compress fields that are interspersed with non-bit fields.

CPP Code
Struct sbit4
{
Int A: 3;
Int B: 4;
Int C;
};
Sizeof (sbit4); // = 8 bytes
Struct sbit4
{
Int A: 3;
Int B: 4;
Int C;
};
Sizeof (sbit4); // = 8 bytes

Sbit4: | a x 3 + B x 4 + # x 1 | # x 8 | # x 8 | # x 8 | C x 8 | C x 8 | C x 8 | C x 8 |

2.4.4.5 the total size of the entire struct is an integer multiple of the size of the widest basic type.

CPP Code
Struct sbit5
{
Int A: 3;
Int B;
Int C: 5;
};
Sizeof (sbit5); // = 12 bytes
Struct sbit5
{
Int A: 3;
Int B;
Int C: 5;
};
Sizeof (sbit5); // = 12 bytes

Sbit5: | A × 3 + # × 5 | # × 8 | # × 8 | # × 8 | B × 8 | B × 8 | B × 8 | B × 8 | c × 5 + # × 3 | # × 8 | # × 8 | # × 8 |

2.5 Union
Union indicates that some data members take one of them. Therefore, memory is allocated in superposition mode, occupying the maximum number of bytes of data members.
CPP Code
Union u
{
Int I;
Char C;
Double D;
};
Sizeof (U); // = max (sizeof (I), sizeof (C), sizeof (D) = sizeof (d) = 8

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.