简体   繁体   English

为什么我们在指针算术和数组地址中使用(字节)而不是(位)?

[英]Why do we use (bytes) instead of (bits) in pointers arithmetic and array's addresses?

Why is each element of an int array separated by (4)?为什么 int 数组的每个元素由 (4) 分隔?

I watched and read many info on pointers arithmetic, but they don't really explain what happen under the hood, they say " an int is 4 bytes" and then they just add that to the address to get the next value.我看了很多关于指针算术的信息,但他们并没有真正解释幕后发生的事情,他们说“一个 int 是 4 个字节”,然后他们只是将其添加到地址以获得下一个值。

But how are we adding a (hexadecimal address) to a (byte) to go from (0x7fff2c70e670 +4) to ( 0x7fff2c70e674)但是我们如何将(十六进制地址)添加到(字节)到 go 从(0x7fff2c70e670 +4)到(0x7fff2c70e674)

shouldn't 4 bytes be converted first to hexadecimal before adding?在添加之前不应该先将 4 个字节转换为十六进制吗?

I feel like I am missing something but I can't point to it.我觉得我错过了什么,但我不能指出它。

int main(void)
{
    int numbers[5] = {1, 3, 5, 7, 9};

    for (int i = 0; i < 5; ++i)
    {
        printf("%d = %p \n",numbers[i], &numbers[i]);

       
    }
} 

Result结果

1 = 0x7fff2c70e670 1 = 0x7fff2c70e670

3 = 0x7fff2c70e674 3 = 0x7fff2c70e674

5 = 0x7fff2c70e678 5 = 0x7fff2c70e678

7 = 0x7fff2c70e67c 7 = 0x7fff2c70e67c

9 = 0x7fff2c70e680 9 = 0x7fff2c70e680

shouldn't 4 bytes be converted first to hexadecimal before adding?在添加之前不应该先将 4 个字节转换为十六进制吗?

Shouldn't 4 apples be converted to hexadecimal before adding? 4个苹果相加前不应该转成16进制吗?

Doesn't really make sense, does it?真的没有意义,是吗? Bytes are a thing ;字节是一回事 a conceptual thing rather than a physical thing like an apple, but still a thing.一个概念上的东西而不是像苹果这样的物理东西,但仍然是一个东西。

Hexadecimal is just a way to represent numbers.十六进制只是表示数字的一种方式。 0xFF and 255 are the same number, but one is represented in hex and one in decimal. 0xFF 和 255 是相同的数字,但一个以十六进制表示,一个以十进制表示。

If you have 0x1A (or 26) apples and you add 4 apples you have 0x1E (or 30) apples.如果你有 0x1A(或 26)个苹果,你添加 4 个苹果,你就有 0x1E(或 30)个苹果。 If you have 0x7fff2c70e670 (or 140733938984560) bytes and you add 4 bytes you have 0x7fff2c70e674 (140733938984564) bytes.如果您有 0x7fff2c70e670(或 140733938984560)字节,并且添加 4 个字节,则您有 0x7fff2c70e674(140733938984564)字节。

A computer uses neither of these internally, they're just for our convenience.计算机在内部不使用这些,它们只是为了我们的方便。 It uses binary.它使用二进制。 So 0xFF and 255 are 11111111 (sort of, it gets complicated ).所以 0xFF 和 255 是 11111111(有点复杂)。

It doesn't have to be this way.不必是这样的。 But it's the way all "byte addressable" machines work, and those are by far the most popular type today.但这是所有“字节可寻址”机器的工作方式,而且这些机器是目前最流行的类型。

The basic idea — and it is a basic idea, there's nothing secret or fancy or obscure about it — is just that you represent the computer's memory as a giant array of bytes.基本思想——这一个基本思想,没有什么秘密、花哨或晦涩的——就是将计算机的 memory 表示为一个巨大的字节数组。 Each byte has an address, from 0 up to however many bytes (or kilobytes, or gigabytes) of memory you have in your computer.每个字节都有一个地址,从 0 到您计算机中 memory 的任意字节数(或千字节或千兆字节)。 Each byte is individually addressable, so the addresses increase by 1 from one byte to the next.每个字节都是可单独寻址的,因此地址从一个字节到下一个字节递增 1。

Although, many objects you might want to store in memory are bigger than one byte.虽然,您可能想要存储在 memory 中的许多对象都大于一个字节。 For example, a 32-bit integer is going to be four bytes.例如,一个 32 位的 integer 将是四个字节。

This picture may help, taken straight from your program and the result you showed:这张照片可能会有所帮助,直接取自您的程序和您显示的结果:

address地址 contents内容
0x7fff2c70e670 0x7fff2c70e670 1 1个
0x7fff2c70e671 0x7fff2c70e671 0 0
0x7fff2c70e672 0x7fff2c70e672 0 0
0x7fff2c70e673 0x7fff2c70e673 0 0
0x7fff2c70e674 0x7fff2c70e674 3 3个
0x7fff2c70e675 0x7fff2c70e675 0 0
0x7fff2c70e676 0x7fff2c70e676 0 0
0x7fff2c70e677 0x7fff2c70e677 0 0
0x7fff2c70e678 0x7fff2c70e678 5 5个
... ... ... ...

Or it's much easier to see if we look at it four bytes at a time:或者一次看四个字节会更容易看:

0 0 1 1个 2 2个 3 3个
0x7fff2c70e670 0x7fff2c70e670 1 1个 0 0 0 0 0 0
0x7fff2c70e674 0x7fff2c70e674 3 3个 0 0 0 0 0 0
0x7fff2c70e678 0x7fff2c70e678 5 5个 0 0 0 0 0 0
0x7fff2c70e67c 0x7fff2c70e67c 7 7 0 0 0 0 0 0
0x7fff2c70e680 0x7fff2c70e680 9 9 0 0 0 0 0 0

When we look at a picture like this we can't ignore the fact that the byte order is little-endian , that is, the least-significant byte of a 4-byte integer is stored at the lowest address.当我们看这样一张图时,我们不能忽略字节顺序小端字节序的事实,即4字节integer的最低有效字节存储在最低地址。 We can see this more clearly if we use some larger numbers:如果我们使用一些更大的数字,我们可以更清楚地看到这一点:

int numbers2[] = {0x01020304, 0x05060708, 0x090a0b0c};

This might give us这可能会给我们

0 0 1 1个 2 2个 3 3个
0x7fff2c70e700 0x7fff2c70e700 0x04 0x04 0x03 0x03 0x02 0x02 0x01 0x01
0x7fff2c70e704 0x7fff2c70e704 0x08 0x08 0x07 0x07 0x06 0x06 0x05 0x05
0x7fff2c70e708 0x7fff2c70e708 0x0c 0x0c 0x0b 0x0b 0x0a 0x0a 0x09 0x09

But at the end of the day, it's the bytes that have addresses, not the individual bits.但归根结底,具有地址的是字节,而不是单个位。

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

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