关于字节对齐,最早是在《高质量程序设计》中看到的,当时明白一点,就是因为定义的数据字节大小不一(1字节,2字节,4字节,8字节),在内存中可能会有字节对齐的操作,就是数据在内存中的排放,不一定是连续的。因此也产生了一个疑问,就是对于数组而言,特别是结构体,如果不连续排放了。那么,当我们将结构体,当做连续的一段内存来访问时,
: 0x00000000 0x00000000 0x00000000 0x00000000
0x804972c
: 0x00000000 0x00000000 0x00000000 0x00000000
0x804973c: 0x00000000 0x00000000 0x00000000 0x00000000
0x804974c: 0x00000000 0x00000000 0x00000000 0x00000000
0x804975c: 0x00000000 0x00000000 0x00000000 0x00000000
0x804976c: 0x00000000 0x00000000 0x00000000 0x00000000
0x804977c: 0x00000000 0x00000000 0x00000000 0x00000000
0x804978c: 0x00000000 0x00000000 0x00000000 0x00000000
(gdb) s
18 j = sizeof(struct Str);
(gdb) p j
$2 = 0 '\0'
(gdb) s
19 a.i = 0x12;
(gdb) p j
$3 = 12 '\f'
(gdb) s
20 a.j = 0x21;
(gdb)
21 a.l = 0x68;
(gdb)
23 memcpy(p, &a, j);
(gdb)
24 for(i = 0; i < j; i++ )
(gdb) x /32 0x804971c
0x804971c
: 0x00000021 0x63636363 0x63631268 0x00000000
0x804972c
: 0x00000000 0x00000021 0x63636363 0x63631268
0x804973c: 0x00000000 0x00000000 0x00000000 0x00000000
0x804974c: 0x00000000 0x00000000 0x00000000 0x00000000
0x804975c: 0x00000000 0x00000000 0x00000000 0x00000000
0x804976c: 0x00000000 0x00000000 0x00000000 0x00000000
0x804977c: 0x00000000 0x00000000 0x00000000 0x00000000
0x804978c: 0x00000000 0x00000000 0x00000000 0x00000000
: 0x00000000 0x00000000 0x00000000 0x00000000
0x804972c
: 0x63636312 0x00000021 0x63636368 0x63636363
0x804972c
: 0x00000000 0x63636312 0x00000021 0x63636368
0x804973c : 0x63636363 0x00000000 0x00000000 0x00000000
0x804974c: 0x00000000 0x00000000 0x00000000 0x00000000
0x804975c: 0x00000000 0x00000000 0x00000000 0x00000000
0x804976c: 0x00000000 0x00000000 0x00000000 0x00000000
0x804977c: 0x00000000 0x00000000 0x00000000 0x00000000
0x804978c: 0x00000000 0x00000000 0x00000000 0x00000000
(gdb) +12>
1989tie_959541171 2013-2-26 13:39
1989tie_959541171 2013-2-26 13:21
allen_zhan_752827529 2013-2-25 12:12
allen_zhan_752827529 2013-2-25 12:02
1989tie_959541171 2013-2-21 20:53
1989tie_959541171 2013-2-21 20:21
allen_zhan_752827529 2013-2-21 16:29