C # Struct memory alignment

Source: Internet
Author: User
C # Struct memory alignment

Struct:
Struct MyStruct0
{
Public byte id;
Public int val;

}

Output organization body information:
Static void ShowStructInfo0 ()
{
Unsafe
{
Int size = sizeof (byte );
Console. WriteLine ("sizeof ({0}) = {1}", typeof (byte). FullName, size );
Size = sizeof (int );
Console. WriteLine ("sizeof ({0}) = {1}", typeof (float). FullName, size );
Size = sizeof (MyStruct0 );
Console. WriteLine ("sizeof ({0}) = {1}", typeof (MyStruct0). FullName, size );

MyStruct0 myStruct = new MyStruct0 () {id = 251, val = 12000 };

Int I = 0;

Byte * p = (byte *) & myStruct;
Console. writeLine ("myStruct0 start: Address: {0: X2} Value: {1: d}", (int) p, (byte) (* (p + I )));
Console. WriteLine ();
For (I = 0; I <size; I ++)
{
Console. writeLine ("Address: {0 }={ 1: X} Value: {2: d}", I, (int) (p + I), (byte) (* (p + I )));
}

}
}
Output:
 
Sizeof (System. Byte) = 1
Sizeof (System. Single) = 4
Sizeof (structApp. MyStruct0) = 8
MyStruct0 start: Address: 12F440 Value: FB

Address: 0 = 12F440 Value: FB
Address: 1 = 12F441 Value: 00
Address: 2 = 12F442 Value: 00
Address: 3 = 12F443 Value: 00
Address: 4 = 12F444 Value: E0
Address: 5 = 12F445 Value: 2E
Address: 6 = 12F446 Value: 00
Address: 7 = 12F447 Value: 00

Analysis:
(1) alignment is based on the largest type in struct. The address requirements of each field in struct are multiples of 4.
0 (id address start), 3 (placeholder), 4 (val address start)
(2) 1st bytes to store Byte, 2nd ~ 4 bytes for placeholder, 5th ~ 8 bytes for int Storage

(3) because 12000 = hex 2EE0, and Address [4] = E0, Address [5] = 2E, we can see that the cpu of the local machine is small.
2EE0 = 2*16*16*16 + 14*16*16 + 14*16 + 0*16 =

Problem:
Address: 4 = 12F444 Value: E0
Address: 5 = 12F445 Value: 2E
We can describe int. What are the two used?
Address: 6 = 12F446 Value: 00
Address: 7 = 12F447 Value: 00
It should be a negative complement expression.

Background:
MSB (Most Significant Byte, maximum valid bytes) is [Xw-1, Xw-2 ,... xw-8]; LSB (Least Significant Byte, minimum valid bytes) is [X7, X6 ,..., x0]. the remaining bytes are located between MSB and LSB.

Who is the lowest address in the memory of LSB and MSB, that is, who represents the address of the object? This leads to the problems of Big Endian and Little Endian.

If the LSB is in front of msb and the lsb is a low address, the machine is a small end; otherwise, the machine is a large end. digital Equipment Corporation (now part of Compaq) and Intel generally use small-end machines. IBM, Motorola, and Sun generally use big ends. of course, this does not represent all situations. some CPUs can work on small ends and big ends, such as ARM, PowerPC, and Alpha. for details, refer to the processor manual.

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.