Common use of C + + sizeof

Source: Internet
Author: User
Tags array length

Common usage of sizeof 1. The amount of memory that the base type occupies
Type 32-bit system (bytes) 64-bit System (bytes)
Char 1 1
Int 4 4
Short 2 2
Long 4 8
Float 4 4
Double 4 8
Pointer 4 8

The above table is the amount of memory that the base type occupies on different platforms, such as sizeof (int) = 4.

2.sizeof compute the number of bytes occupied by the array

Using sizeof to calculate the number of bytes consumed in an array is calculated by the type of bytes * Array length .

For example:

1 int m_anums[3]; 2 sizeof (m_anums);

The number of bytes calculated using sizeof for M_anums is 12.

3.sizeof calculating the size of a single-layer structure

Member variables in structs can be of different types and are stored in memory sequentially, in the order in which they are declared. Unlike arrays, the size of a struct is not a simple addition to the size of a member variable.

The number of bytes in a struct's memory size is calculated from the offset of the last member variable in the struct and the byte size of the last member variable .

There are two rules for calculating the number of bytes in a struct:

(1) The offset of the member variable within each struct must be an integer multiple of the variable's size.

(2) The size of the computed structure must be an integer multiple of the size of each member variable in the structure, that is, the value of the structure size is the least common multiple of the size of each member variable.

For example:

1typedefstruct {  2     intA; 3     DoubleC; 4      Shortb; 5}sa;6  7typedefstruct {  8     intA; 9      Shortb; Ten     DoubleC;  One}SB;

Result: sizeof (SA) = 24;sizeof (SB) = 16.

Analysis:

Note: The offset of each member variable within the struct = The previous member variable offset + The size of the previous member variable

SA structure body: offset of a = 0,a size = 4;

The offset of C = the size of 8,c = 8;(note: The offset of C cannot be 4, so 4 is not an integer multiple of the size of C, so the offset of C must be 4 bits into 8)

Offset of B = size of 16,b = 2;

The result of the calculation is 24, cannot be 18, because 18 is not 4 and 8 of the least common multiple, so the final result is the need to fill the 6 bits to get its least common multiple 24.

SB structure body: The offset of a = 0,a size = 4;

Offset of B = size of 4,b = 2;

The offset of C = the size of the 8,C = 8;(note: The offset of C cannot be 6 because 6 is not an integer multiple of the C size, so the 2 bit is changed to 8)

The result of the calculation is 16.

From the calculation size of the SA and SB two structures, it is shown that the variable declaration order in the structure can result in different size of the structure.

4.sizeof Calculating the size of nested structuressizeof calculates rules for calculating the size of nested structures:

(1) The embedded structure is expanded, and the offset of the first member variable of the expanded struct is an integer multiple of the largest member variable size in the expanded structure.

(2) The size of the final computed structure must be an integer multiple of the size of all member variables, where all member variables are calculated as expanded variables instead of embedding the struct as a whole.

For example:

struct SC  {        short  i;         struct          {             char  C;              int J;        } SS;          int k;  }  

sizeof (SC) = 16, where the offset of SS.C is 4, not 2.

Common use of C + + 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.