Knowledge---V

Source: Internet
Author: User

1. Memory Alignment

Before explaining the effect of memory alignment, consider the rules for memory alignment:

1), for each member of the struct, the first member is positioned at offset 0, and the offset for each data member must be a multiple of min (#pragma the number specified by the pack (), the data member's own length).

2), after the data members have completed their respective alignment, the structure (or union) itself is aligned, and the alignment will be performed according to the value specified by the #pragma pack and the maximum data member length of the structure (or union), whichever is smaller. (The default is 8, so that's the number of bytes in the structure for the maximum length member)

The #pragma pack (n) indicates that the alignment is set to n bytes. VC6 default 8-byte alignment

struct c{    bool  C1;     int C2;     BOOL c3;}; struct d{    int  d1;     BOOL D2;     BOOL D3;};

sizeof (C): A, sizeof (D): 8

C in Memory:
|bool|---|---|---|
|-------int------|
|bool|---|---|---|
D in Memory:
|-------int------|
|bool|bool|---|---|

structa1{intA; Static intb;};structa2{intA; Charc;};structa3{floatA; Charc;};structa4{floatA; intb; Charc;};structa5{DoubleD; floatA; intb; Charc;};

sizeof (A1) = 4: Because the static variable is present in the global data area, and sizeof calculates the size allocated in the stack, B is not counted.

*************************************************************************************************************** ****************************

* About five large memory partitions
* in C + +, memory is divided into 5 zones, which are heap, stack, free storage, global/static storage, and constant storage.
The stack is the store of variables that are allocated by the compiler when needed, and that are automatically clear when not needed. The variables inside are usually local variables, function parameters, and so on.
* Heap, which is the memory blocks allocated by new, their release compiler does not go to the tube, by our application to control, generally a new will correspond to a delete. If the programmer does not release it, the operating system will automatically recycle after the program finishes.
* Free storage, which is a chunk of memory allocated by malloc and so on, is very similar to the heap, but it ends up living with no.
* Global/static storage, global variables and static variables are allocated in the same piece of memory, in the previous C language, the global variables are divided into initialized and uninitialized, in C + + There is no such distinction, they occupy the same chunk of memory.
* Constant storage, this is a more special storage area, they are stored in a constant, not allowed to modify (of course, you have to be through the improper means can also be modified, and many methods, in the "Const thinking" article, I gave 6 ways)

*************************************************************************************************************** ****************************

sizeof (A2) = 8: To take care of data alignment

sizeof (A3) = 8: To take care of data alignment

sizeof (A4) = 12: To take care of data alignment

sizeof (A5) = 24:

The first step: Members are aligned according to rule 1, accounting for 20 bytes. 0 (Double)->8 (float)->12 (int)->16 (char).

The second step: the struct itself is aligned according to rule 2, sizeof (A5) = Pack_default min (8, sizeof (double)) = min (8) =3* 8 = 24.

Knowledge---V

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.