Since each single hexadecimal digit corresponds to 4 bits and each byte of memory requires 2 hexadecimal digit, Why the increment in the memory addresses in the code below is happening in the nibbles not the whole byte?
#include <iostream>
using namespace std;
int main()
{
int a , b, c, d;
cout << &a << "\t" << &b << "\t" << &c << "\t" << &d << endl;
cout << (long) &a << "\t" << (long)&b << "\t" << (long) &c << "\t" << (long)&d << endl;
return 0;
}
As you see in the output below the increment in memory addresses for our int type happened only in nibbles. For example the first increment is from 58 to 5c (4 nibbles from 8 to c) and 4 nibbles is only 2 bytes not 4.
output:
0x7ffdc94b6e58 0x7ffdc94b6e5c 0x7ffdc94b6e60 0x7ffdc94b6e64
140727980617304 140727980617308 140727980617312 140727980617316
CodePudding user response:
140727980617304 140727980617308
As you can see, the difference between these two addresses is 4. Pointer addresses are, basically, memory addresses. These addresses are 4 bytes apart. Your int
s take up four bytes long. Hence, consecutive int
s, stored in memory, will be 4 addresses apart.
4 nibbles from 8 to c
You are conflating a term used to describe the representation of a single hexadecimal number with memory addresses.
"nybble" is to hexadecimal is as "digit" is to our natural base-10 number representation.
Whether each particular memory address is presented as a hexadecimal number, consisting of nybbles, or a decimal addresses consisting of digits, makes no difference whatsoever. The above two memory addresses differ by a value of 4, whether you choose to view them as hexadecimal nybbles or decimal digits. The memory addresses are 4 addresses apart. The End.