127k views
2 votes
Assume that a little-endian machine currently has the data shown (in hex) below in its memory:addr: 0x48 0x49 etc. data: | AC | EF | AC | CA | FE | CA | 0D | F0 |Which address (in hex) should be put in the blank below so that the variable y holds the value 0xCAFE?short *p = ______;short y = *p;

User Kfaerber
by
6.9k points

1 Answer

7 votes

The address that should be put in the blank so that the variable y holds the value 0xCAFE is 0x48 + 2 = 0x4A

This is because the machine is little-endian, which means that the least significant byte of a multi-byte value is stored at the lowest memory address. In this case, the least significant byte of 0xCAFE is 0xFE, and it is stored at the address 0x48 + 2.

So, the correct code would be:

short *p = (short *) 0x4A;

short y = *p;

User Pavlo Viazovskyy
by
8.1k points