Live data from Hacker News

Why do arrays start at 0?

buttondown.email

61–70 of 702 posts

Re: Why do arrays start at 0?

#61

Earlier quoted context omitted.

> when we count, we start at 1, we talk about the "1st" Although often with an implicit zero. Under typical North American culture, your 1st birthday, for example, is more accurately the first anniversary of your birthday. Your birth is zero indexed.

No, birthdays are 1 indexed. Your Nth birthday is the day you turn N years old. When you're 1 year old, you've been alive for 1 year, not 2 years.

No, old calendar systems are one indexed. In those system, there is literally no year zero; the first year is year one. This leads to crazy things like year 100 being part of the "first century" and year 101 being part of the "second century".

That is not the case with age or birthdays which are, thankfully, zero indexed. The first year of human life is age=0, birthdays=0.

Re: Why do arrays start at 0?

#62

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…

Is it machine-focused that we can convert a distance of 1.5 m to 150 cm just by multiplying by 100? Maybe distances should be human-focused, so that no displacement is equivalently expressed as 1 cm, 1 m, 1 km, ... Then converting a distance d from m to cm is (d - 1) x 100 + 1.

... but if you have something that's 150cm, that's not 151cm, that's 150cm?

Re: Why do arrays start at 0?

#63

> The usual arguments involving pointer arithmetic Yeah, I think this exactly why it is zero based. The index became a simple multiplier for sizeof(int).

An assembler spillover into C.

Was C ever much more than a veneer on top of assembler?

Don't get me wrong, it's why I like C.

Re: Why do arrays start at 0?

#64

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's just convention though. There's no reason "[x]" couldn't have been defined to be "+ (x - 1) * sizeof". There would be no runtime cost to this, and the compile time penalty would be tiny even on ancient systems.

Is `(x - 1)` not a runtime cost if `x` is a runtime variable?

Re: Why do arrays start at 0?

#65

Earlier quoted context omitted.

So how old is a not-yet-1-year old?

N days or N months depending on how old the baby is.

And before an hour has even passed, we'd probably say "minutes old" or perhaps "not even an hour/day old", all to avoid saying "zero days/months/years old". But some might still say zero days old, and they'd be both understood and correct (at least logically if not stylistically). That's the "implicit zero" everybody is talking about. We avoid saying it, but that's just a convention of communcation. Logically it's there. You're zero days old before you're one day old.

Re: Why do arrays start at 0?

#66
post #48

Indexes start at 1. Offsets start at 0. Arrays in programming use offsets. for(int i=0;i The error here is calling it 'i' for index. It should be 'o' for offset.

Arrays in the programming languages you’re familiar with use offsets.

Not all do [1].

1. https://therenegadecoder.com/code/which-programming-language...

Re: Why do arrays start at 0?

#67
This reminds me of arguments about male -> female and man -> woman (and human etc). Without offending anyone's delicate sensibilities - you may note that there is a short version and long version of those words, suggesting that the longer version is derivative of the short version.

However, when you examine the origin of the words, that is not the derivation. The derivation and path to English is quite complicated. You may feel a sense of righteous indignation or righteous repudiation on this topic. I would gently suggest deferring that feeling because I think the story is very interesting to follow.

That is not the end of the story though. The reason those pairs of words stuck around is almost certainly because it makes a consistent mental picture.

Bringing it back to the topic at hand - "Why we use 0" - because it works. There are many times when it has been evaluated, and at least to the people who design languages, it is more mentally appealing.

https://www.etymologynerd.com/blog/man-vs-woman

https://medium.com/interesting-histories/interesting-histori...

Re: Why do arrays start at 0?

#68
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.

That is true for vectors, but not matrices, right?

  char at(char matrix[10][10], char i, char j) {
    return matrix[i][j];
  }
still has more computation on 1-indexed than on 0-indexed, I believe.

Re: Why do arrays start at 0?

#69

Earlier quoted context omitted.

> when we count, we start at 1, we talk about the "1st" Although often with an implicit zero. Under typical North American culture, your 1st birthday, for example, is more accurately the first anniversary of your birthday. Your birth is zero indexed.

Yeah age is confusing even for non-computer-folks :) If you have 4 classes in school today, you would never talk about the 1st class as number 0, the last one is the 4th, not the 3rd.

Although, there is a fifth state in your example: When you are not in class. Which is different to an empty set that implies nothingness. When it comes to age, 0 being birth works well because there is truly is nothingness (from your perspective) before birth. When counting from 1 there is suggestion that there are variables that aren't worth speaking of because they are obvious.

Re: Why do arrays start at 0?

#70

Edsger Dijkstra wrote an interesting article titled 'why numbering should start at 0'. Perhaps not answering the question directly but an interesting read nonetheless. https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...

As the article implies, Dijkstra was specifically wrong about FORTRAN, as defined at the time — the '77 standard, when it was still only conscionable to SHOUT 6 alphamerics.

“… you probably know that arrogance in computer science is measured in nano-Dijkstras.” — Alan Kay

Post reply on HN