c++ - CSAPP打印程序对象的字节表示问题?
PHP中文网
PHP中文网 2017-04-17 14:35:01
0
1
633

CSAPP中有一段打印对象的字节表示的代码,代码如下:

typedef unsigned char * byte_pointer;
void show_bytes(byte_pointer start, int len) {
    int i;
    for (i = 0; i < len; i++)
        printf("%.2x ", start[i]);
    printf("\n");
}

int main()
{
    int x = 12345;
    show_bytes((byte_pointer) &x, sizeof(int));
}

请问这段代码中,强制转化发生了什么过程,为什么能够得到对象的字节表示?

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(1)
PHPzhong

x is an int value, which generally occupies 4 bytes. In the memory layout, you can think of this value as a byte array of length 4. For example, x=12345, which is 0x3039. In the memory layout, the layout of Data can be considered as start[0] as 0x00 and start[2] as 0x30. Of course, depending on the CPU endianness, the byte order will be reversed.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template