Live data from Hacker News

Why do arrays start at 0?

buttondown.email

1–10 of 702 posts

Re: Why do arrays start at 0?

#4
Arrays start at 0 because they're really just pointers. "[x]" is just shorthand for "+ x * sizeof". The first element is the one stored at the pointer address (0 sizeofs ahead of the pointer, the next element is stored 1 sizeof ahead of the pointer, etc.

Re: Why do arrays start at 0?

#5

Arrays start at 0 because they're really just pointers. "[x]" is just shorthand for "+ x * sizeof". The first element is the one stored at the pointer address (0 sizeofs ahead of the pointer, the next element is stored 1 sizeof ahead of the pointer, etc.

That answers something like "what are the technical benefits to zero-based arrays" but not "why are zero-based arrays such a strong convention in programming languages." For that you'd have to read the article I guess.

Re: Why do arrays start at 0?

#6

I have always assumed that is just one less operation required to resolve the absolute memory address.

No.

1. Optimizing compilers exist.

2. Addressing at fixed offsets is cheap (a single instruction): https://en.wikipedia.org/wiki/Addressing_mode

0-based indexing is superior (as a default) because it simplifies a lot of common math, as discussed in TFA.

Re: Why do arrays start at 0?

#7
I found it interesting that the author phrased their critique so definitely, i.e. "it's NOT this and NOT that", yet concluded the article by saying their whole argument was speculative. That said, I do agree with their premise that it's going to be pretty tough to get a definitive answer as to why 0-indexing won out. And their criticism of others being so ready to declare results is apt, if not a little ironic in this case :)

Re: Why do arrays start at 0?

#8
There is definitely a bunch of math that works out better in 0 based indices. I saw this a lot when I was working on filesystem focused stuff.

I feel like I've also seen algorithms that work out better with 1 based indices, but not as many.

Re: Why do arrays start at 0?

#9

I have always assumed that is just one less operation required to resolve the absolute memory address.

The mental model of being an offset to a memory address is, I think, part of it.

I'd be surprised if the use of zero vs one actually made any difference, from a number of operations point of view -- from the compiler's point of view, the first element in the array is the first element in the array, no matter what we call it.

I mean in an extreme edge case maybe if you are computing an index, and it happens to be zero, then in your math maybe some identity related to zero could be exploited (go to element simple_integer*complicated_function(), where simple_number might be zero) but that seems a bit silly.

Re: Why do arrays start at 0?

#10

I have always assumed that is just one less operation required to resolve the absolute memory address.

This is primarily the actual answer. While most languages force you to see the array as an array, in C/C++ (in C++ I'm only referring to the memory allocated variable type, not an "array class") you can see it as a pointer and do your own math to address any part of it that you want. And the calculation for that memory address is idx * sizeof(the thing in your array). So the real answer here is that array semantics needed to match memory address semantics in the languages that provide that.
Post reply on HN