# 本博客已经迁移至：

http://cenalulu.github.io/

# 本篇博文已经迁移，阅读全文请点击：

http://cenalulu.github.io/linux/why-array-start-from-zero/

## 最早的原因

If a BCPL variable represents a pointer, it points to one or more consecutive words of memory. These words are the same size as BCPL variables. Just as machine code allows address arithmetic so does BCPL, so if p is a pointer p+1 is a pointer to the next word after the one p points to. Naturally p+0 has the same value as p. The monodic indirection operator ! takes a pointer as it’s argument and returns the contents of the word pointed to. If v is a pointer !(v+I) will access the word pointed to by v+I.

## 为什么这个反人类设计在一段时间内一直没有被改变

So: the technical reason we started counting arrays at zero is that in the mid-1960’s, you could shave a few cycles off of a program’s compilation time on an IBM 7094. The social reason is that we had to save every cycle we could, because if the job didn’t finish fast it might not finish at all and you never know when you’re getting bumped off the hardware because the President of IBM just called and fuck your thesis, it’s yacht-racing time.

## 一些现代语言为什么仍然使用这种做法

Using 0-based indexing, half-open intervals, and suitable defaults (as Python ended up having), they are beautiful: a[:n] and a[i:i+n]; the former is long for a[0:n].
Using 1-based indexing, if you want a[:n] to mean the first n elements, you either have to use closed intervals or you can use a slice notation that uses start and length as the slice parameters. Using half-open intervals just isn't very elegant when combined with 1-based indexing. Using closed intervals, you'd have to write a[i:i+n-1] for the n items starting at i. So perhaps using the slice length would be more elegant with 1-based indexing? Then you could write a[i:n]. And this is in fact what ABC did -- it used a different notation so you could write a@i|n.(See http://homepages.cwi.nl/~steven/abc/qr.html#EXPRESSIONS.)

## 总结

• 在计算资源缺乏的过去，0标号的写法可以节省编译时间
• 现代语言中0标号可以更优雅的表示数组字串
• 在支持指针的语言中，标号被视作是偏移量，因此从0开始更符合逻辑

### 参考文献

posted @ 2015-02-11 15:53  cenalulu  阅读(13444)  评论(5编辑  收藏