This article tests how to store int-type data in x86:The high address bit stores the int's high level, and the low address bit stores the int's low level.This storage method is calledSmall-end format.
There is also the "big-end format ".
Read oneProgram:
Code
# Include " Stdio. h "
Void Main ()
{
Int Ival = 65536 ;
Char * P = & Ival;
Printf ( " * P: % d \ n " , * P );
Printf ( " * P + 1: % d \ n " , * (P + 1 ));
Printf ( " * P + 2: % d \ n " , * (P + 2 ));
Printf ( " * P + 3: % d \ n " , * (P + 3 ));
Printf ( " * P + 4: % d \ n " , * (P + 4 ));
// Print pointer address
Printf ( " P: % P \ n " , P );
Printf ( " P + 1: % P \ n " , P + 1 );
Printf ( " P + 2: % P \ n " , P + 2 );
Printf ( " P + 3: % P \ n " , P + 3 );
Printf ( " P + 4: % P \ n " , P + 4 );
}
Output result:
* P: 0
* P + 1: 0
* P + 2: 1
* P + 3: 0
* P + 4:-52
P: 0012ff70
P + 1: 0012ff71
P + 2: 0012ff72
P + 3: 0012ff73
P + 4: 0012ff74
Read a exercise question:
Exercise
1 # Include " Stdio. h "
2 Void Main ()
3 {
4 Union { Char A [ 10 ];
5 Int I;
6 } U;
7
8 Int * P = ( Int * ) & (U. [ 1 ]); // Note that a [1] is not a [0].
9 * P = 65536 ;
10 Printf ( " U. I = % d \ n " , U. I );
11 Printf ( " U. A [0] = % d \ n " , U. [ 0 ]);
12 Printf ( " U. A [1] = % d \ n " , U. [ 1 ]);
13 Printf ( " U. A [2] = % d \ n " , U. [ 2 ]);
14 Printf ( " U. A [3] = % d \ n " , U. [ 3 ]);
15 Printf ( " U. A [4] = % d \ n " , U. [ 4 ]);
16 }
Output result:
U. I = 16777420
U. A [0] =-52
U. A [1] = 0
U. A [2] = 0
U. A [3] = 1
U. A [4] = 0
Reference for byte order: http://hi.baidu.com/santy/blog/item/0cb6024fd0634730aec3ab09.html