在C中,数组定义中的长度如何映射到寻址?

时间:2022-11-27 16:59:26

In C, when I define an array like int someArray[10], does that mean that the accessible range of that array is someArray[0] to someArray[9]?

在C中,当我定义一个类似int someArray [10]的数组时,这是否意味着该数组的可访问范围是someArray [0]到someArray [9]?

3 个解决方案

#1


4  

Yes, indexing in is zero-based, so for an array of n elements, valid indices are 0 through n-1.

是的,c中的索引是从零开始的,因此对于n个元素的数组,有效索引是0到n-1。

#2


2  

Yes, because C's memory addressing is easily computed by an offset

是的,因为C的内存寻址很容易通过偏移来计算

myArray[5] = 3

roughly translates to

粗略地翻译成

store in the address myArray + 5 * sizeof(myArray's base type)
the number 3.

Which means that if we permitted

这意味着如果我们允许的话

myArray[1]

to be the first element, we would have to compute

要成为第一个元素,我们必须计算

store in the address myArray + (5 - 1) * sizeof(myArray's base type)
the number 3

which would require an extra computation to subtract the 1 from the 5 and would slow the program down a little bit (as this would require an extra trip through the ALU.

这需要额外的计算来从5中减去1,并且会使程序慢一点(因为这需要通过ALU额外的行程)。

Modern CPUs could be architected around such issues, and modern compilers could compile these differences out; however, when C was crafted they didn't consider it a must-have nicety.

现代CPU可以围绕这些问题进行架构,现代编译器可以将这些差异编译出来;然而,当C被精心制作时,他们并不认为这是必须的精确。

#3


1  

Think of an array like this:

想想这样的数组:

* 0   1   2   3   4   5   6   7   8   9
+---+---+---+---+---+---+---+---+---+----+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
+---+---+---+---+---+---+---+---+---+----+
                DATA

* = array indices

So the the range of access would be [0,9] (inclusive)

所以访问范围是[0,9](含)

#1


4  

Yes, indexing in is zero-based, so for an array of n elements, valid indices are 0 through n-1.

是的,c中的索引是从零开始的,因此对于n个元素的数组,有效索引是0到n-1。

#2


2  

Yes, because C's memory addressing is easily computed by an offset

是的,因为C的内存寻址很容易通过偏移来计算

myArray[5] = 3

roughly translates to

粗略地翻译成

store in the address myArray + 5 * sizeof(myArray's base type)
the number 3.

Which means that if we permitted

这意味着如果我们允许的话

myArray[1]

to be the first element, we would have to compute

要成为第一个元素,我们必须计算

store in the address myArray + (5 - 1) * sizeof(myArray's base type)
the number 3

which would require an extra computation to subtract the 1 from the 5 and would slow the program down a little bit (as this would require an extra trip through the ALU.

这需要额外的计算来从5中减去1,并且会使程序慢一点(因为这需要通过ALU额外的行程)。

Modern CPUs could be architected around such issues, and modern compilers could compile these differences out; however, when C was crafted they didn't consider it a must-have nicety.

现代CPU可以围绕这些问题进行架构,现代编译器可以将这些差异编译出来;然而,当C被精心制作时,他们并不认为这是必须的精确。

#3


1  

Think of an array like this:

想想这样的数组:

* 0   1   2   3   4   5   6   7   8   9
+---+---+---+---+---+---+---+---+---+----+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
+---+---+---+---+---+---+---+---+---+----+
                DATA

* = array indices

So the the range of access would be [0,9] (inclusive)

所以访问范围是[0,9](含)