Memory alignment, big endian byte order small end byte order verification

Source: Internet
Author: User

Empty structure: for the empty structure, it is only the structure of the mold, but there is no element of the structure.

Cases:

typedef struct STUDENT

{

}STD;

The mold of this empty structure takes up one byte, and sizeof (std) = 1.


Flexible arrays:

The last element in the struct can be an array of unknown size, called a flexible array member, which specifies that there is at least one element in front of the flexible array.

typedef struct STUDENT

{

int i;

Char arr[]; Flexible array Members

}STD;

sizeof (STD) = 4;

sizeof the size of the struct is not included in the size of the flexible array, the flexible array regardless of size is not counted into the size of the structure, you can use dynamic memory for it to achieve memory allocation.


650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m00/7f/3f/wkiol1cxmtvsnbgwaaaddrz4ibm189.png "title=" Untitled 2.png "alt=" Wkiol1cxmtvsnbgwaaaddrz4ibm189.png "/>

Memory Alignment:

For words (the natural boundary is an even address), the double word (the natural boundary is an address divisible by 4), and four words (the natural boundary is the address divisible by 8) is itself aligned. Why the alignment? This is because only one memory access is required for aligned memory, and for unaligned memory, the processor has two memory accesses.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s1.51cto.com/wyfs02/m02/7f/42/wkiom1cxmlstam61aabfurzgn-q952.png "title=" Untitled 3.png "alt=" Wkiom1cxmlstam61aabfurzgn-q952.png "/>

unaligned: A word or double word spans a 4-byte boundary, or a double word spans a 8-byte boundary and requires two memory accesses.


For structs, a union considers its memory alignment when calculating its size, aligned with the type type with the largest number of bytes in the structure.

Cases:

struct test

{

Char A1;

Short B2;

Char C3;

int d4;

};

Because the int type takes up the largest number of bytes, it is aligned in 4 bytes, and the memory is allocated in the following way, totaling 12 bytes:

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m01/7f/40/wkiol1cxmzur9_ybaaahdivhuew706.png "title=" Untitled 4.png "alt=" wKioL1cXmZuR9_ Ybaaahdivhuew706.png "/>

Analysis: Because of the 4-byte alignment, the first char A1 takes one byte to 00, and the short B2 is the word Two bytes , aligned with even addresses, so it cannot be stored directly on 01 02, but should be stored on 02 03, and the empty 01 will be wasted. Similarly, when Char C3 is saved to 04, for int i, the double word must be saved to a multiple of 4 address, It can only be stored on the 0a 0b, empty out of 05 06 07 will be wasted.


struct test

{

Char A1;

Char C3;

Short B2;   

int d4;

};

Similarly, in 4-byte alignment, a total of 8 bytes:

This is because A1 saved to 00,c3 to 01,b2 just deposited on 02 03, aligned with even addresses, D4 is just starting from 04 storage, 4 byte aligned, no wasted memory.



Big endian byte order: High byte is stored at low address, low byte is stored at high byte

Small endian: low byte stored at low address, high byte stored at high address

The minimum storage unit for a computer is bytes, and one byte occupies 8bit bits.

Take int as an example:

For example: 1 of binary codes are

00000000 00000000 00000000 00000001

Written in 16 binary form: 00 00 00 01 This represents 4 bytes, and the memory is from the low address to the high address, resulting in two kinds of storage methods.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s2.51cto.com/wyfs02/m02/7f/40/wkiol1cxmcfxkortaaa805lwhxu219.png "title=" Untitled. png "alt=" Wkiol1cxmcfxkortaaa805lwhxu219.png "/>

Because of the way they are stored, there are two ways to read them.

The general reading is read from the low address to the high address, in order to unify the storage and read, so the small-end storage, so that the low-byte memory to the low address, high-byte to high address.

Verification Method:

1, you can declare an int a=1, and then declare a char *p=&a; because a is 4 bytes, char * accesses one byte at a time, so if it is a small end, the output *p on the computer should be 1.

2. You can also use Union because the union is memory-common, so declare an int a,char C; Assign a value of 1 to a, and output C to observe the result.

Some problems are not understood at all without regard to the size of the end.


This article is from the "11132019" blog, please be sure to keep this source http://11142019.blog.51cto.com/11132019/1766032

Memory alignment, big endian byte order small end byte order verification

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.