所以我看到,当变量在c++中声明时,如果您想获得最佳的缓存读取,那么内存应该坚持它的自然对齐。示例:
int a; // memory address should end in 0x0,0x4,0x8,0xC
int b[2]; // 8 bytes 0x0,0x8
int b[4]; // 16 bytes 0x0
但在实践中,这些变量并不遵循“自然对齐”规则,一个16字节变量驻留在以0xC结尾的内存地址中。这是为什么?
我读过中的memcpy实现,发现下面的代码很难理解,有人能解释一下吗?
36 /*
37 * sizeof(word) MUST BE A POWER OF TWO
38 * SO THAT wmask BELOW IS ALL ONES
39 */
40 typedef long word; /* "word" used for optimal copy speed */
41
42 #define wsize sizeof(word)
43 #define wmask (wsize - 1)
44
..。
/*