1) Little-Endian is the low-byte emission at the low-address end of the memory, and the high-byte emission at the High-address end of the memory.
2) Big-Endian refers to the low address of high bytes discharged in the memory, and the low byte is discharged in the high address of the memory.
16-bit-width 0x1234 in Little-endian mode and Big-endian mode) the storage mode of CPU memory is assumed to start from address 0x4000:
Memory Address |
Content storage in Small-end Mode |
Content storage in big-end Mode |
Zero x 4000 |
0x34 |
0x12 |
Zero x 4001 |
0x12 |
0x34 |
C Language Program for determining the size
Inti = 1;
Char * p = (char *) & I;
If (* p = 1)
Printf ("LittleEndian ");
Else
Printf ("BigEndian ");
The root cause of the need for byte alignment is the efficiency of CPU access data. The system accesses data from 0-3, 3-7, and other addresses at a time. If an int type data is stored in 2, 3, at, the memory needs to be accessed twice. if the address is 0, 1, 2, and 3, the memory only needs to be accessed once.
1. for a type of data, the starting address of the data to be stored must be an integer multiple of the size of the data type. If it is inta, a only needs to be stored in address 0, 4, 8, 12 ..... The int value can be 4 bytes ). If charb is used, it is stored in 0, 1, 2, 3, 4, 5 ,... You can start from any address. If shortb is used, it is stored in, 8... .
Float is 4 bytes, and double is 8 bytes.In fact, alignment is considered in the struct. It is of little significance to define a variable or array separately.
2. struct is the largest alignment of the defined data type.
Structtest
{
Charx1;
Charx4;
Limit X2;
Charx5;
};
The result after running is sizeof (test) = 6, instead of 5.
0: x11: x42-3: x24: x5
Address 0 storage x1 address 1 storage x4 address 2, 3 storage x2 address 4 Storage x5
This mainly takes into account the issue of struct arrays. The size of the struct must be an integer multiple of the space occupied by the largest variable.
Structtest
{
Charx1: 2;
Charx4: 6;
Charx2: 7;
Charx5: 4;
};
Sizeof (test) = 3;
If it is changed
Structtest
{
Charx1: 2;
Charx4: 7;
Charx2: 7;
Charx5: 4;
};
Sizeof (test) = 4;
You only need to ensure that the same character variable is in one byte.
This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1304010