C + + Knowledge point 1: Memory alignment

Source: Internet
Author: User

Preliminary knowledge: Basic type occupancy byte

How many bytes are the basic data types on 32-bit operating systems and 64-bit operating systems?

32-bit operating system:

Char:1 int:4 short:2 unsigned int:4 long:4 unsigned long:4 long long:8 float:4 Dou Ble:8 Hands: 4

64-bit operating system

Char:1 int:4 short:2 unsigned int:4 long:8 unsigned long:8 long long:8 float:4 Dou Ble:8 Hands: 8

Memory Alignment:

Member alignment has an important condition in which each member is aligned in its own way. The rules for aligning are: Each member is aligned to the lesser of its type's alignment parameters (usually the size of this type) and the specified alignment parameters (here by default, 8 bytes). And the length of the structure must be an integer multiple of all the alignment parameters used. Fill in the empty bytes if not enough.

Example:

struct t{    long  A;      Short b;     int C;     int *D;      Char e;}

The size of the 64-bit operating system.

Analysis:

Allocate memory space in the order in which they are declared.

First long variable A, in the 64-bit address space, the long type is 8 bytes, so according to the above alignment conditions, this member should be according to its parameter min (sizeof (long), 8) = 8 bytes to align, so the member is stored in the 0~7 memory unit.

Then short variant B, in the 64-bit address space, the short type occupies 2 bytes, so according to the above alignment conditions, this member should be according to its parameter min (sizeof (short), 8) = 2 bytes to align, so the member is stored in the 8~9 memory unit.

then the int variable C, in the 64-bit address space, the int type occupies 4 bytes, so according to the above alignment condition, this member should be aligned to its parameter min (sizeof (int), 8) = 4 bytes, so the member is stored in the 12~15 memory unit (10, 11 units are not divisible by 4).

Then the int* variable D, in the 64-bit address space, the pointer type is 8 bytes, so according to the above alignment conditions, this member should be according to its parameter min (sizeof (int*), 8) = 8 bytes to align, so the member is stored in the 16~23 memory unit.

Then char-type variable e, in the 64-bit address space, the char type is 1 bytes, so according to the above alignment condition, this member should be according to its parameter min (sizeof (char), 8) = 1 bytes to align, so the member is stored in 24 memory unit.

The length of the entire structure must then be an integer multiple of all alignment parameters, the current length is 25, not all alignment parameters integer times, and must be adjusted to 32, which is the integer multiple of all parameters.

So the length of the structure is 32.

  

What if a sub-structure appears in the struct? When we determine the alignment parameters of a sub-struct, it should be the largest of the alignment parameters used by all its members.

Example:

struct t{    Char  A;     int b;}; struct s{   char  C;    struct t D;    Char e;};

The length under the 32-bit operating system.

First, the size of T is 8, and all its members use a maximum of 4 alignment parameters.

Re-examine S:

First char variable C, in the 32-bit address space, the char type is 1 bytes, so according to the above alignment conditions, this member should be according to its parameter min (sizeof (char), 8) = 1 bytes to align, so the member is stored in 0 memory unit.

then struct T variable D, in the 32-bit address space, the struct T is 8 bytes, so according to the above alignment condition, this member should be aligned to its parameter 4 bytes, so the member is stored in the 4~11 memory unit.

Then char-type variable e, in the 32-bit address space, the char type is 1 bytes, so according to the above alignment condition, this member should be according to its parameter min (sizeof (char), 8) = 1 bytes to align, so the member is stored in 12 memory unit.

The length of the entire structure must then be an integer multiple of all alignment parameters, the current length is 13, not all alignment parameters integer times, and must be adjusted to 16, which is the integer multiple of all parameters.

So the size of this struct is 16.

C + + Knowledge point 1: Memory alignment

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.