It is stored in Small-end mode, and its memory layout is as follows:Variable I occupies 4 bytes, but only one byte value is 1, and the value of the other three bytes is 0. If the value of the low address is 0, there is no doubt that this is the big end mode; if the value of the low address is 1, there is no doubt that this is the small end mode. In this case, we can fully utilize the characteristics of the union data: the starting addresses of all Members are consistent.
Now, how should I write it? The answer is as follows:
int checkSystem( ){ union check { int i; char ch; } c; c.i = 1; return (c.ch ==1);}
Now you can use this function to test the storage mode of your current system. Of course, you can also directly view the memory without using a function to determine the storage mode of the current system. For example:
The value 0x01 in the figure has a low address, indicating that the current system is in the small-end mode.
However, some systems may support both storage modes. You can use hardware jumpers or set the storage mode in compiler options.
Leave a question: what is the output value in the x86 system? <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> #include intmain(){ int a[5]={1,2,3,4,5}; int *ptr1=(int *)(&a+1); int *ptr2=(int *)((int)a+1); printf("%x,%x",ptr1[-1],*ptr2); return 0;}
Answer: * ptr1 should be easier to judge.
Sizeof (a) = 20 a [5] is 4*5 = 20 bytes
The memory content is:
a ptr[-1] &a+101 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00 00
& A + 1 indicates that 20 bytes are added at a time, and ptr1 [-1] = offsets one byte at a time. Therefore, ptr [-1] = 5
(Int) What is the difference between a + 1 and & a + 1? note that very large (int) a + 1 means to change the address of a to an integer and then add 1, here, the offset address only logically adds a unit.
a (int)a+1 &a+101 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00 00
At this time, * ptr2 points to four bytes 00 00 02 because the computer adopts the small-end mode to forcibly convert 00 00 02 to int *, it becomes 02 00 00 00 00.
Note: 00 00 00 02 the address here increases sequentially, so 02 is located at the high address