简体   繁体   中英

Why does a pointer increment in nibbles instead of bytes in an array?

I don't understand why a pointer increments in nibbles instead of bytes in arrays.

For example, consider a 32 bit, little endian machine and an array of 4 ints (1, 2, 3, 4)

Addresses -> (Value stored in hex)

0x002BF94C -> 01
        4D -> 00
        4E -> 00
        4F -> 00

0x002BF950 -> 02
        51 -> 00
        52 -> 00
        53 -> 00

...And so on

The space between 4C and 4D is a nibble, yet each points to a byte in virtual memory. How is this possible? Shouldn't the pointers increment by a byte?

The space between 4C and 4D is a nibble

No, these addresses are expressed as bytes. 4D - 4C is 1 byte, not 1 nibble.

yet each points to a byte in virtual memory. How is this possible? Shouldn't the pointers increment by a byte?

The pointer values are bytes. Incrementing them by 1 increments them by a byte.

Basically I have no idea where you got the idea that anything here is expressed in nibbles, but they are not. Go back to thinking in bytes.

As other answers have said, the pointers here are incrementing in bytes, not nibbles.

I believe you're inferring a relationship that doesn't exist between the addresses and the actual chunks of memory.

In a byte-addressable machine , each byte has its own address. You increment by one to get to the next address. So you have the address 0x002BF94C, and the next address is 0x002BF94D. While the change in the addresses only affects the lowest nibble of the address itself, that has nothing to do with the chunk of memory that the address points to. Since this machine is apparently byte-addressable, the number of bits between the chunk addressed by 0x002BF94C and the one addressed by 0x002BF94D is, in fact, 8 bits, or one byte.

The choice of what to use as addresses is arbitrary. The people who designed the architecture could have chosen to give each byte a name, street number, and zip code, but it was obviously more convenient to give them numbers. The choice of what sized chunk of memory gets its own address is also arbitrary. Some machines of the past didn't have byte-addressable memory; they had word-addressable memory, so only groups of 16 or 32 bits (or whatever size the makers wanted) got their own addresses. You could theoretically have a nibble-addressable machine, although I don't think anyone has made one. There is no necessary relation between the address and how much memory it refers to.

The diagram in the question is probably adding to the confusion. Remember that 32 bits is equivalent to 4 bytes, so each 32-bit number has four addresses pointing to its various parts. Each addressed segment in the diagram is a byte; if they were nibbles, the integers would only have two bytes and would be 16-bit, not 32-bit. Since the numbers are in hexadecimal there, each digit represents a nibble, so 01 is actually two nibbles / eight bits / one byte, 0000 0001 .

0x002BF94C -> 01  This is one byte
        4D -> 00  This is the next byte
        4E -> 00  etc.
        4F -> 00

If this machine were nibble-addressable, the diagram would look like this:

0x002BF94C -> 1  This is one nibble
        4D -> 0  This is the next nibble
        4E -> 0  etc.
        4F -> 0
        50 -> 0
        51 -> 0
        52 -> 0
        53 -> 0

Is it, possibly, because each integer on your system has 4 bytes in it? Say, in C:

int array[10];
int *pA = array;

printf("pA= %x", (size_t)pA);
pA++;
printf("pA= %x", (size_t)pA);

But why nibble?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM