A summary of the size of bytes in VS

Source: Internet
Author: User

Description: With regard to byte alignment, many people gave the answer by setting the #pragma pack (1) to operate. However, in situations where space usage is not a concern, or if the protocol package is not being sent, this setting is not performed in order for the program to run faster.

The following are the default byte alignments

1. In a struct, the number of bytes used in the alignment is the maximum amount of bytes that are searched for keywords in the struct (the default minimum is 4 bytes)

Like what:

struct Node {

int A;

Double b;

};

The size of sizeof (Node) is 16 bytes instead of the so-called 12 bytes, because the number of bytes in the struct keyword (double) is 8 bytes, so the size is byte-aligned.

If this is the case:

struct Node {

int A;

Double b;

char c;

}

Then the size of sizeof (Node) is 24 bytes.

2, regarding the member function pointer occupies the space size, the concrete can see the following link

Http://www.linuxeden.com/html/news/20141206/157482.html

Note that if this is the case:

Class Base;

typedef (BASE::* Func) ();

Then func takes up 16 bytes

If

Class Base;

Class base{};

typedef (BASE::* Func) ();

Then Func takes up only four bytes. This means that as long as the class Base is defined in a typedef (BASE::* Func) (), it will simply take up a simple corresponding number of bytes.

3. What happens if the member function pointers are also put into the structure?

Declare a class here

Class Base;

typedef (BASE::* Func) ();

Through the 2nd link we know here the size of sizeof (Func) is 16 bytes

So here's an example

struct Node {

int A;

func func;

Double b;

}

How much space does sizeof need to occupy? The answer is 8+16+8 = 32 bytes. Since the structure is calculated with the largest footprint, why is it 32 bytes instead of 48 bytes? In fact, the alignment is the keyword (double) that is defined by C + +, as I said above. So it takes only 32 bytes.

A summary of the size of bytes in VS

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.