1. 程式人生 > >為什麼陣列標號是從0開始的 • cenalulu's Tech Blog

為什麼陣列標號是從0開始的 • cenalulu's Tech Blog

本文通過彙總一些網上搜集到的資料,總結出大部分程式語言中陣列下標從0開始的原因

背景

我們知道大部分程式語言中的陣列都是從0開始編號的,即array[0]是陣列的第一個元素。這個和我們平時生活中從1開始編號的習慣相比顯得很反人類。那麼究竟是什麼樣的原因讓大部分程式語言陣列都遵從了這個神奇的習慣呢?本文最初是受stackoverflow上的一個問題的啟發,通過蒐集和閱讀了一些資料在這裡做個總結。當然,本文摘錄較多的過程結論,如果你想把這篇文章當做快餐享用的話,可以直接跳到文章末尾看結論。

最早的原因

在回答大部分我們無法解釋的詭異問題時,我們最常用的辯詞通常是歷史原因。那麼,歷史又是出於什麼原因,使用了0標號陣列呢?

Mike Hoye就是本著這麼一種追根刨地的科學精神為我們找到了解答。以下是一些他的重要結論的摘錄翻譯:

據作者的說法,C語言中從0開始標號的做法是沿用了BCPL這門程式語言的做法。而BCPL中如果一個變數是指標的話,那麼該指標可以指向一系列連續的相同型別的數值。那麼p+0就代表了這一串數值的第一個。在BCPL中陣列第5個元素的寫法是p!5,而C語言中把寫法改成了p[5],也就是現在的陣列。具體原文摘錄如下:

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.

至於為什麼C語言中為什麼使用[]方括號來表示陣列下標,這個設計也有一定來歷。據C語言作者的說法是方括號是現代鍵盤上唯一較為容易輸入的成對符號(不用shift)不信你對著鍵盤找找?

為什麼這個反人類設計在一段時間內一直沒有被改變

根據Mike的說法,BCPL是被設計在IBM硬體環境下編譯執行的。在1960後的很長一段時間內,伺服器硬體幾乎被IBM統治。一個城市內也許至於一臺超級計算機,還需要根據時間配額使用。當你當天的配額用完以後,你的程式就被完全清出計算佇列。甚至連計算結果都不給你保留,死無全屍。這個時候寫一段高效的程式,就顯得比什麼都重要了。而這時0下標陣列又體現了出了它的另一個優勢,就是:相較於1下標陣列,它的編譯效率更高。原文摘錄如下:

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.

此外,還有另外一種說法。在C語言中有指標的概念,而指標陣列標號實際上是一個偏移量而不是計數作用。例如對於指標p,第N個元素是*(p+N),指標指向陣列的第一個元素就是*(p+0)

一些現代語言為什麼仍然使用這種做法

上文中提到的為了計較分秒的編譯時間而使用0下標陣列,在硬體飛速發展的今天顯然是不必要的。那麼為什麼一些新興語言,如Python依然選擇以0作為陣列第一個元素呢?難道也是歷史原因?對於這個問題,Python的作者Guido van Rossum也有自己的答案。這裡大致概括一下作者的用意:從0開始的半開放陣列寫法在表示子陣列(或者子串)的時候格外的便捷。例如:a[0:n]表示了a中前n個元素組成的新陣列。如果我們使用1開始的陣列寫法,那麼就要寫成a[1:n+1]。這樣就顯得不是很優雅。那麼問題來了,Python陣列為什麼使用半開放,即[m,n)左閉合右開發的寫法呢?這個理解起來就比較簡單,讀者可以參考http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF作為擴充套件閱讀。下面摘錄一段Python作者的原話:

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 [email protected]|n.(See http://homepages.cwi.nl/~steven/abc/qr.html#EXPRESSIONS.)

總結

從0標號的陣列傳統,沿用了這麼長時間的原因主要列舉如下:

  • 在計算資源缺乏的過去,0標號的寫法可以節省編譯時間
  • 現代語言中0標號可以更優雅的表示陣列字串
  • 在支援指標的語言中,標號被視作是偏移量,因此從0開始更符合邏輯

參考文獻

  1. (http://developeronline.blogspot.com/2008/04/why-array-index-should-start-from-0.html)
  2. [http://exple.tive.org/blarg/2013/10/22/citation-needed/]
  3. [https://plus.google.com/115212051037621986145/posts/YTUxbXYZyfi]
  4. [http://stackoverflow.com/questions/7320686/why-does-the-indexing-start-with-zero-in-c]