Data alignment/Memory alignment && data format

Source: Internet
Author: User

One, data alignment/memory alignment

Word, double word, four words do not need to be aligned in memory on natural boundaries. (What is called a word?) See part II)

For the word, double word, four words, the natural boundary is even address, can be divisible by 4 address, can be divisible by 8 address.

In any case, in order to improve the performance of the program, data structures (especially stacks) should be aligned as far as possible on natural boundaries.

The reason is that, in order to access unaligned memory, the processor needs to do two memory accesses; however, the aligned memory requires only one access.

Misaligned: A word or double-word operand spans a 4-byte boundary, or a four-word operand spans 8-byte boundaries, is considered unaligned, and requires two bus cycles to access memory.

Aligned: A word start address is odd but does not span a word boundary that is considered aligned and can be accessed during a bus cycle.

By default, the compiler defaults the memory alignment of the structure, the member data in the stack.

three rules to align :

1. Each member is aligned according to his own way;

2. The default alignment of a complex type, such as a structure, is the alignment of its longest member. (equivalent to expanding a complex type)

3. The length of the alignment must be an integer multiple of the largest alignment parameter in the member. (guarantees that each item is aligned when the array is processed)

Example

1. For the following structure

struct teststruct1{  char C1;  Short 2;  char C2;  int I;};
Assuming that the address of C1 is 0, then C1 is 00000000,s for 00000002,C2 to 00000004,i to 00000008.

2. For the following structure

struct teststruct2{  char A;  Long b;}; struct teststruct3{  char c;  TestStruct2 D;  Long long e;};

In TestStruct2, member A is 1 bytes, the default is 1-byte aligned, member B is 4 bytes, and by default is 4-byte aligned. So sizeof (TESTSTRUCT2) is 8.

In TestStruct3, member C is aligned by 1 bytes, and D is a struct, which is 8 bytes, but the default alignment of a struct is the largest of the alignment parameters used by all its members, so d is aligned by 4 bytes, and E is 8 bytes, which is aligned by 8 bytes by default. The memory layout is as follows: (* indicates free memory, 1 means memory is used)

TESTSTRUCT3 Memory Layout: 1***,1***,1111,****,11111111

C D.A d.b E

Another: for typedef char ARRAY3[3],ARRAY3 This type of alignment is still aligned by 1 bytes, not by its length.

Reference: "C Language Depth Analysis" 3rd Chapter preprocessing, #pragma pack

#pragma pack (n) ...//This middle section aligns #pragma pack () by the specified alignment

However, member alignment has an important condition in which each member is aligned in its own way. (That is, although the alignment is specified by n-byte, not all members are aligned in N-bytes.) The alignment rule is that each member is aligned by its type's alignment parameters (usually the size of this type) and the smaller one in the specified alignment parameter, and the length of the structure must be an integer multiple of all the alignment parameters used, and not enough to fill the empty bytes.

Second, the data format

Because it is expanded from 16-bit architectures to 32-bit, Intel uses the term word to represent 16-bit data types. Because, the 32-digit number is " double word " (double words) and the 64-digit number is " Four words " (quad words).

Contrast--word length

Each computer has a word size , which indicates the nominal size of the integer and pointer data.

Because the virtual address is encoded in one of these words (it is my understanding that the length of the word size varies by 01 to represent the virtual address), so the most important system parameter is the maximum size of the virtual address space determined by the length of the message.

That is, for a machine with a word length of W, the range of virtual addresses is 0~2^w-1 and the program accesses up to 2^w bytes. Most computers today have a 32-bit word length, which limits the virtual address space to 4GB bytes.

4

C declaration

Char

Short

int

Long int

Long Long

pointer

float

Double

32-bit machine

1

2

4

4

8

4

8

64-bit machine

1

2

4

8

8

8

4

8

can see that

    • Short integers are assigned 2 bytes,
    • The non-qualifying int is 4 bytes,
    • The "Long" integer uses the machine's full word length (32 bits are 4 bytes and 64 bits are 8 bytes).
    • The "long" integers introduced by ISO C99 use 8 bytes, or 64-bit integers. (for 32-bit machines, the compiler must compile the operation of this data type into code that performs a series of 32-bit operations.) )
    • Pointer, using the full word length of the machine.
    • For floating-point numbers, single-and double-precision uses 4 bytes and 8 bytes, respectively.
About "word length" and "words"

The word length is the nominal size of the computer's integer and pointer data, which may be 32 or 64 digits, and so on, while the character refers to 16 bits, because the double word is 32 bits and four words is 64 bits. Be careful not to confuse the concept of "word length" and "word", meaning "words" is not the length of "character", but refers to the nominal size of computer integers and pointer data.


Data alignment/Memory alignment && data format

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.