Live data from Hacker News

Why do arrays start at 0?

buttondown.email

631–640 of 702 posts

Re: Why do arrays start at 0?

#631
0-based indexing is clearly the best option because it results in more elegant code and it matches what computers actually do.

In fact, it's so fundamentally more elegant that I've come to the conclusion that the real issue is that it's actually the mathematicians and language itself that got indexing wrong. Instead of "first" being associated with 1, it should be associated with 0. We should give athletes 0th place, and talk about the 0th man on the moon etc.

That's where the cognitive dissonance for the 1-based-indexing people is coming from. They can't deal with the fact that their "normal" way of indexing is wrong.

It's a bit like how pi should really be tau (2*pi), or the electron should really be positive. We got it wrong, but we're stuck with it because it's too much of a hassle to change it. Fortunately computing got it right! But now there's a mismatch with everyday life where people are used to the wrong thing.

Re: Why do arrays start at 0?

#632

Earlier quoted context omitted.

The same applies to counting in other bases too. For instance, in 1-indexed counting grids for kids, the last column always feels out of place. 0-indexed decimal grid: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 8…

Are you sure it doesn't just look that way because you are used to monospaced fonts? Ask a kid to show you their zeroth finger.

> Ask a kid to show you their zeroth finger.

They will get confused because every day counting got it wrong. In a perfect world we would count fingers from 0.

Re: Why do arrays start at 0?

#633
post #603
post #596

Earlier quoted context omitted.

when I count apples I happily start with one. it's "natural" to label the first thing "1". or how would you count, say, 3 Apples? or three legionaries?

Counter-example: You have to remove all desks from #20 to #30, how many desks are there? Oh, it's 11 desks, I see. This is because we used INCLUSIVE indexing, instead of semi-open interval. If we use semi-open intervals, we don't include the last item, so that we can write match index { 0..10 => println!("first ten"), 10..20 => println!("next ten"), _ => println!("something else"), } but this forces us to start our i…

Indeed. End exclusion is advantageous because we don't need to add or subtract 1 when constructing or interpreting ranges. Forgetting to add or subtract 1 is basically why off-by-one errors exist (and doing add rather than subtract or vice versa is probably why off-by-two errors exist).

`a..(a+n)` is an n-length range, not an (n + 1)-length range (oh look, an "add 1" operation!).

And an empty range is denoted by `a..a`, not `a..(a-1)` (oh look, a "subtract 1" operation!).

Re: Why do arrays start at 0?

#634
"It's an offset from the beginning. The first element is 0 slots from the beginning." is what I was taught. (Of course, it's more complicated than that, with pointer math etc etc, but that works as a general gist)

In the past, I've written roughly half a million lines of Lua. Arrays don't always start at 0. I've regularly shaken my fist at Lua and cursed it's wicked ways, but it's really damn useful in a lot of contexts.

Re: Why do arrays start at 0?

#635
post #603
post #596

Earlier quoted context omitted.

when I count apples I happily start with one. it's "natural" to label the first thing "1". or how would you count, say, 3 Apples? or three legionaries?

Counter-example: You have to remove all desks from #20 to #30, how many desks are there? Oh, it's 11 desks, I see. This is because we used INCLUSIVE indexing, instead of semi-open interval. If we use semi-open intervals, we don't include the last item, so that we can write match index { 0..10 => println!("first ten"), 10..20 => println!("next ten"), _ => println!("something else"), } but this forces us to start our i…

doesn't swift use inclusive indexing? 1...10 includes 10

Re: Why do arrays start at 0?

#636

Earlier quoted context omitted.

For constant addressing; for arr[i] = 2, you'll still need to subtract 1 from i with 1-based addressing when converting to machine instructions.

> for arr[i] = 2, you'll still need to subtract 1 You can, but don't need to. Just have compiler store array pointer constant as (arr-1) instead of arr, et voila, zero runtime overhead. Or, for modern(-ish) ISAs, often you can add/substract a small constant at runtime, with no extra cycles taken. For example, for x86_64: # rbp contains "true" pointer to arr # rax contains 1-based array index mov rax, QWORD PTR [rbp-8…

Another option for users is to "sacrifice" the 0th entry of an array. Depending on the size(as in sizeof) of the entry, it can be worth it.

A benefit is that you can then use 0 as a sentinel value; for instance if you have a find() routine that surely can fail, it can just return 0 instead of having e.g. -1 (which can introduce minor issues).

In my experience, though, I am so used to 0-based index that switching schemes can cause stupid off-by-one bugs. I guess that's the main reason behind complains about Lua. It's not that "natural" arrays are thought of as bad, but mixing both schemes (often C an Lua) is error-prone.

Re: Why do arrays start at 0?

#638
post #53
post #30

Earlier quoted context omitted.

> BTW on which level is the 1st floor? in Europe or in the US?

Does Europe has a zero floor? Please tell me Europe zero-indexes their stories!

Yeah, most or all of Europe does that. In English it is called ground floor, first floor, etc. In Swedish the zero numbering makes total sense becasuse instead of numbering the floors we say ground floor, "1 stair", "2 stairs", etc.

Re: Why do arrays start at 0?

#639
post #635
post #603

Earlier quoted context omitted.

Counter-example: You have to remove all desks from #20 to #30, how many desks are there? Oh, it's 11 desks, I see. This is because we used INCLUSIVE indexing, instead of semi-open interval. If we use semi-open intervals, we don't include the last item, so that we can write match index { 0..10 => println!("first ten"), 10..20 => println!("next ten"), _ => println!("something else"), } but this forces us to start our i…

doesn't swift use inclusive indexing? 1...10 includes 10

Haven't used Swift, but looking at the documentation, it seems to support `...` for end-inclusive and `..Looking at the Swift standard library, end-exclusive ranges seem to be far more commonly used:

  ~/build/swift$ git grep -e '\.\.

Re: Why do arrays start at 0?

#640

Because memory starts at zero, because the lowest address you can select in memory has all its address lines set to zero. If you don't understand this, you should not be writing computer software.

Welcome to pagination and virtualization where physical address is different than what your program think is. Also welcome to hypervisors where entire operating systems think they own an entire physical line, only to have something like Spectre/Meltdown say otherwise.
Post reply on HN