Live data from Hacker News

Why do arrays start at 0?

buttondown.email

231–240 of 702 posts

Re: Why do arrays start at 0?

#231
post #97

Earlier quoted context omitted.

Ask 100 random people on the street and I'd be surprised if even 1 knew the definition of "ordinal". It's an uncommon word.

Do people not study grammar in American schools? I thought all kids learn the distinction between cardinal (one, two, three) and ordinal (first, second, third) numbers.

[deleted]

Re: Why do arrays start at 0?

#232

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…

How would it not make a difference? If you calculate an index at runtime, to get access to the element, in 0 based would be pointer + index. In 1 based it is however pointer + index - 1 clearly there is an extra subtraction there?

In x86 you could probably hide it in the addressing but that does not mean it does not to be computed

Re: Why do arrays start at 0?

#233
post #168

If you ask people which floor of building they're on, it's going to depend on which country they're in. In North America, at least, the first floor you walk into (in a sane city: I understand there are some which do not qualify in this respect due to hills or historic disaster recovery) is the first floor. On other continents, you enter the ground floor and need to take stairs or an elevating device to get to the fir…

I always said it was an argument between the counting of things versus the relative offset of things.

This is a great example.

Re: Why do arrays start at 0?

#234
post #97

Earlier quoted context omitted.

Ask 100 random people on the street and I'd be surprised if even 1 knew the definition of "ordinal". It's an uncommon word.

Do people not study grammar in American schools? I thought all kids learn the distinction between cardinal (one, two, three) and ordinal (first, second, third) numbers.

At the age that cardinal and ordinal numbers are taught, kids simply don't remember "cardinal" and "ordinal", they remember "one, two, three" and "first, second, third".

99% of the instances I've seen "ordinal" outside of this thread has been in code/documentation. It is not a common word in everyday language.

Re: Why do arrays start at 0?

#235
I don't quite understand the argument "0-based being easier for pointer arithmetic is nonsense because the language doesn't have pointers".

Whether or not the language presents the concept of "pointer" to the user is independent of whether or not it uses pointers internally. And if it exposes arrays as a concept, it has to implement them somehow.

The simplest possible implementation of arrays is having a start address and putting all elements next to each other in RAM. To get the address of a particular item, this layout naturally leads to the formula "base address + index * element size", with "index" being 0-based. If you want to expose other indexing schemes in your language, you'll have to add more logic to convert the user-visible index back to 0-based before you can obtain the address.

All of this is completely independent of the fact whether your language exposes pointers to the user or not.

Even the yacht story sort of hints at this:

> To keep people from having their jobs cut short by yacht-racing, Richards designed the language to compile as fast as possible. One optimization was setting arrays to start at 0.

If all indexing schemes were equal, why would this change even be an optimisation in the first place?

Re: Why do arrays start at 0?

#237

I've discussed this a lot in real world in a different field: apartment floors. In Japan (where I live) they are 1-indexed, where the floor on the ground is number 1, while in Spain (where I am from) they are 0-indexed, where the floor on the ground is number 0. Both have inconsistencies, like in Spain you might have a "middle ground" (entresuelo) which is neither 0 nor 1, but sits between, and is normally commercial…

English also has that "mezzanine" concept, for partial floors with a balcony over the main floor. You see it sometimes on elevators as an M.

Re: Why do arrays start at 0?

#239

Unfortunate that the Julia folks weren't exposed to this when they designed the language.

https://juliaarrays.github.io/OffsetArrays.jl/stable/

0-based or any arbitrary offset of your choice so that your data model can more closely map to what you need it to. And use eachindex instead of making assumptions and you can work with arrays using 1-based, 0-based, or arbitrary-based indexing.

Post reply on HN