Live data from Hacker News

Why do arrays start at 0?

buttondown.email

171–180 of 702 posts

Re: Why do arrays start at 0?

#171
post #26

It really comes down to a choice between a machine-focused (0) or human-focused (1) approach. The 0 makes a lot of sense in a C pointer world where memcpy and other alike functions can be written very thight. The 1 makes a lot of sense in a human world, when we count, we start at 1, we talk about the "1st", counting on finger starts with 1, etc. I once were at a Lua (1 indexed language) conference where this was disc…

If you think of the index as an offset you would start with 0. BTW on which level is the 1st floor?

Nobody knows that one.

No elevator I've seen has yet taken a cue from UI design: simply put the buttons within an outline of the building, along with the local numbering scheme.

No more visits to the serial killer lurking in the basement.

The players in this market evidently have been operating at T'ump levels of intelligence. /s

Re: Why do arrays start at 0?

#173
post #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.

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

If that was true, Fortran, Julia, R and Matlab would use 0 instead of 1-based.

Re: Why do arrays start at 0?

#175
I always assumed that it was because arrays and pointers would've been interchangeable in the start, and that adding a zero index times the object size gives you the first element

Re: Why do arrays start at 0?

#176

Earlier quoted context omitted.

A disadvantage comes to mind. While the following loop works as expected: for (size_t i = 0; i The following causes an unsigned integer underflow and is an infinite loop: for (size_t i = length - 1; i >= 0; i--) ...

In that specific case I'd do the following: for (size_t i = n; i-- > 0 ;) ... Or count from `length` to 1, but subtract 1 in the loop body, or count up and subtract the length in the loop body. Any modern compiler should be able to optimise these to be equivalent. In the majority of cases, counting down is not necessarily. Nor is ordered iteration. Most languages have a `for each` style syntax that's preferable anywa…

Ah yes, the goes-to operator -->

Re: Why do arrays start at 0?

#178
Another reason can be performance/memory.

When you index starting at 1, you either have to add/subtract 1 internally, which sometimes but not always can be optimized away by a smart JIT/compiler, or you have to "waste" the first element in memory, trading memory for performance.

Lua always subtracts 1, while LuaJIT instead went with the latter approach.

Re: Why do arrays start at 0?

#179
post #45

Other advantages of zero based indexing, beyond being 'closer to the machine': It works better with the modulo operator: `array[i%length]` vs `array[(i+length-1)%length+1]`. Or you would have to define a modulo-like operator that maps ℕ to [1..n]. It works better if you have a multi-dimensional index, for example the pixels in an image. With 0 based indexing, pixel `(x,y)` is at `array[x+width y]`. With 1 based index…

A rare case where 1-based indexing is more convenient is complete binary trees laid out breadth-first (as in a standard binary heap): parent is i div 2 and children are 2i and 2i+1 when starting at one and who knows what when starting at zero. But that’s the only one I know.

Re: Why do arrays start at 0?

#180
post #164

Actually in not all languages do arrays start at 0. In ColdFusion for example arrays start at 1. I've never been able to receive a reason as to why CF's original author, J.J. Allaire, did it that way.

And Julia, an otherwise nice language.
Post reply on HN