一、字节
1个字节(Byte)= 8 位(bit);
举例:
地址 数据
0x00000010 12345678 90abcdef 11223344 55667788
0x00000020 11223344 55667788 12345678 90abcdef
暂时不管大小端,假设地址0x10上的数据是0x12,即00010010,占8位,1个字节;0x11上的数据是0x34,占8位,1个字节…
所以,地址 0x10到0x18之间的数据有12345678 90abcdef,一共占8个字节。
注意:经常有字节对齐。64位默认8字节对齐,32位默认4字节对齐。
在下面的计算中就遇到了,本次测试是linux 64位。
二、结构体地址
struct sgo_down {
int b;//4
struct sgo_up *up;//8
int c;//4
};
因为有8字节对齐,所以 sizeof(struct sgo_down) = 24
struct sgo_up {
int a;//4
struct sgo_down down;
};
同理:sizeof(struct sgo_up) = 32
void func1(struct sgo_up *up)
{
struct sgo_down *test_down;
test_down = &up->down;
test_down->b = 1;
test_down->up = up