Summary of C ++ sizeof and sizeof

Source: Internet
Author: User

Summary of C ++ sizeof and sizeof

The keyword sizeof is used to return the number of memory bytes occupied by an object or type. The returned value is size_t.

Number of bytes occupied by the basic data type: (32-bit System)

Char 1 byte

Bool 1 byte

Short 2 bytes

Int 4 bytes

Long 4 bytes

Float 4 bytes

Double 8 bytes

An important factor affecting the sizeof results is byte alignment. First, let's look at a formula: Valid alignment value = min (Self Alignment value, set alignment value ).

The alignment value is the number of bytes occupied by a data type,

Example:

Int a; // Its alignment value is 4 char c; // Its alignment value is 1 double d; // Its alignment value is 8 // For struct, the alignment value is the largest alignment value in the built-in data type, struct s1 {int q; // 4 bool e; // 1 char w; // 1 }; // The maximum value is int q, so the alignment value is 4 struct s2 {int q; // 4 bool e; // 1 double r; // 8 }; // The maximum value is double r, so the alignment value is 8.

Set the alignment value to the default alignment value of the compiler. The alignment value is vs2013, and the default alignment value is 4 bytes, project ---> Properties ---> Configuration Properties ---> C \ C ++ ---> Code Generation

---> In Struct Member Alignment, you can also set it through macro # pragma pack (n), and n is the Alignment byte to be set.

The valid alignment value is a smaller alignment value of the object or type and a smaller alignment value. It is also the actual alignment value.

After some changes are made in the preceding example:

# Pragma pack (4) // set the alignment value to 4 struct s1 {int q; bool e; char w ;}; // The alignment value of s1 is 4, so the valid alignment value of s1 is min () = 4 # pragma pack (2) // set the alignment value to 2 struct s2 {int q; bool e; double r ;}; // s2's own alignment value is 8, so the effective alignment value of s2 = min (8, 2) = 2

You can easily calculate sizeof by knowing the valid alignment value.

I. sizeof of Basic Data Types

Sizeof (int) = 4

Sizeof (double) = 8

......

Ii. Structure sizeof

# Pragma pack (4) struct s1 {int a; char B ;}; // The valid alignment value is 4 sizeof (s1) = 8 // int a occupies 4 bytes, stored in 0x00 -- 0x03, char B occupies 1 byte, stored in 0x04, because the valid alignment value is 4, so 0x05 -- 0x07 after char B is filled with alignment, a total of 8 bytes are occupied // struct s1 {char B; int a ;}; // The valid alignment value is 4 sizeof (s1) = 8 // The answer is the same, however, the internal storage status has changed. Char B occupies 1 byte and is stored at 0x00. int a occupies 4 bytes, because the valid alignment value is 4 and 0x01 --- 0x03 has only 3 bytes of memory, it is smaller than the number of bytes required by int a, so a new 4-byte memory (valid alignment value) is allocated, finally, int a is stored in 0x04 --- 0x07 // let's look at another example: struct s2 {char a; short B; double c; char d ;}; // The valid alignment value is 4 sizeof (s2) = 16 // char a occupies 1 byte and is stored at 0x00; short B occupies 2 byte, because the valid alignment value is 4, the remaining 3 bytes of memory in the first segment are 0x01 --- 0x03, which is greater than short B. Therefore, short B is stored in 0x02 --- 0x03 (note: the first storage address must be an integer multiple of the member size, so 0x01 is empty); double c occupies 8 bytes, which is greater than the valid alignment value. Therefore, two segments of memory 0x04 --- 0x0B are allocated to store double c; char d occupies 1 byte, stored in 0x0C, 4-byte pairs based on valid alignment values Qi, and the final 0x0D --- 0x0F is filled with alignment. A total of 16 bytes.

3. struct contains the sizeof of struct type

The alignment value of a struct is the largest of its built-in types.

Example:

# Pragma pack (2) class A {public: int a; double s ;}; // The alignment value of A is the alignment value of double s, it is 8 // but the valid alignment value of A is min (8, 2) = 2, sizeof (A) = 12 class B {public: char c; // The alignment value of itself is 1, 1-byte A B; // its own alignment value is 8, which occupies sizeof (A) = 12 bytes}; // B's own alignment value is equal to A's own alignment value, valid alignment value of 8 // B = min (8, 2) = 2, sizeof (B) = 14 // char c occupies 1 byte, it is stored at 0x00 and 0x01; A B occupies 12 bytes. 14 bytes in total.

4. sizeof with struct containing virtual functions

The structure with virtual functions has a virtual table pointer, which is 4 bytes in size.

Example:

# Pragma pack (4) Class A {public: int a; virtual int test () ;}; sizeof (A) = 8 // int a occupies 4 bytes, the virtual table pointer occupies 4 bytes, which are 8 bytes in total. // Note: If a struct has multiple virtual functions, there is still only one virtual table pointer, that is, multiple virtual functions share one virtual table pointer.

For inheritance, if the base class has a virtual function, the virtual table pointer will also be inherited, that is, the base class and the derived class share a virtual table pointer.

Example:

# Pragma pack (4) class A {public: int a; double s; virtual int test () ;}; class B: public A {public: virtual int test_1 (); // shared base-class virtual table pointer virtual int test_2 (); // shared base-class virtual table pointer char c ;}; sizeof (B) = 20 // int a occupies 4 bytes + double s occupies 8 bytes + virtual table pointer occupies 4 bytes + char c occupies 1 byte + 3 bytes complement alignment = 20 bytes

V. sizeof of a consortium

The members in the consortium share the memory. the sizeof of the consortium is the maximum value of sizeof each member.

Example:

union u  {      int a;        double b;      char c;    bool d;};  sizeof(u)=sizeof(b)=8

6. sizeof containing static struct

Static and global variables are stored in the static storage area. Only non-static members are calculated when the sizeof of struct is calculated.

Example:

# Pragma pack (4) class A {public: int a; double B; static int d; // whatever it is}; sizeof () = 12 // int a occupies 4 bytes + double B occupies 8 bytes = 12

VII. sizeof Function

The result is the size of the function return type. Therefore, sizeof cannot be obtained for a function that does not return a value.

Sizeof (function name (real parameter table ))

Example:

Int A () {return 1;} sizeof (A () = sizeof (int) = 4 char B (char B) {return B ;} sizeof (B ('B') = sizeof (char) = 1 void C () {} sizeof (C ()/error, because no response type is returned

If you have any mistakes, I hope you can correct them !! Grateful!

 

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.