简体   繁体   English

了解十六进制和 Memory 地址

[英]Understanding Hexadecimal and Memory addresses

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?由于每个十六进制数字对应4位,而memory的每个字节需要2个十六进制数字,为什么下面代码中memory地址的增量发生在半字节而不是整个字节?

#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.正如您在下面的 output 中看到的,我们的 int 类型的 memory 地址的增量仅发生在半字节中。 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.例如,第一个增量是从 58 到 5c(从 8 到 c 的 4 个半字节),而 4 个半字节只是 2 个字节而不是 4 个字节。

output: output:

0x7ffdc94b6e58  0x7ffdc94b6e5c  0x7ffdc94b6e60  0x7ffdc94b6e64
140727980617304 140727980617308 140727980617312 140727980617316
140727980617304 140727980617308

As you can see, the difference between these two addresses is 4. Pointer addresses are, basically, memory addresses.如您所见,这两个地址之间的差异是 4。指针地址基本上是 memory 个地址。 These addresses are 4 bytes apart.这些地址相隔 4 个字节。 Your int s take up four bytes long.您的int占用四个字节长。 Hence, consecutive int s, stored in memory, will be 4 addresses apart.因此,存储在 memory 中的连续int将相隔 4 个地址。

4 nibbles from 8 to c从 8 到 c 的 4 个半字节

You are conflating a term used to describe the representation of a single hexadecimal number with memory addresses.您将用于描述单个十六进制数的表示形式的术语与 memory 地址混为一谈。

"nybble" is to hexadecimal is as "digit" is to our natural base-10 number representation. “nybble”是十六进制,“digit”是我们自然的 base-10 数字表示。

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.每个特定的 memory 地址是表示为由半字节组成的十六进制数,还是由数字组成的十进制地址,都没有任何区别。 The above two memory addresses differ by a value of 4, whether you choose to view them as hexadecimal nybbles or decimal digits.以上两个 memory 地址相差 4,无论您选择将它们视为十六进制字节还是十进制数字。 The memory addresses are 4 addresses apart. memory地址相隔4个地址。 The End.结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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