Live data from Hacker News

Why do arrays start at 0?

buttondown.email

671–680 of 702 posts

Re: Why do arrays start at 0?

#671

Earlier quoted context omitted.

"Offset-based" is bringing in pointers; that's the thing it's an offset "from". "The beginning of the array" is just a pointer. I suppose saying that does have an advantage over explicitly talking about pointers, in that the word "pointer" is a piece of jargon that has a lot of baggage. That's just avoiding jargon, though, not really using a different model.

No, offset means "measuring from origin", pointers use that language they don't provide it. The advantages in measuring from origin can accrue to the person choosing to do it, because there are other reasons to do so which aren't satisfying the CPU.

No, offset means "measuring from a defined location". In a computer, the choice of location is more or less arbitrary[0]. If you pick the first element, you get zero indexing. If you pick a space before the first element, you get one indexing. Either one is a pointer, since a pointer is just computer jargon for "a defined location".

Calling that defined location "origin" doesn't suddenly make it not a pointer.

[0]- NUMA and cache effects aside, as they don't matter for this purpose

Re: Why do arrays start at 0?

#672

Earlier quoted context omitted.

Quick complement. Where I’m from (Portugal) we use the literal number 0 for the ground floor.

Actually, "rés-do-chão" would be the more common term. Still... in Portugal, the "first floor" is implicitly never the ground floor (but the floor above it), so 0-based indexing is respected (as you suggest).

On elevators you don’t see “R/C” unless the elevator is very old, and the same for office building directories or shopping centres. In almost all cases it’s the number zero. OTOH, in countries like Germany you will see the “E” for Erdgeschoss everywhere.

Re: Why do arrays start at 0?

#673
post #624

Earlier quoted context omitted.

It's basically only x86 among modern ISAs that lets you do base + literal + register * literal, aarch64 only gives you base + register shifted by a literal, and I believe RISC-V is similar: https://gcc.godbolt.org/z/dz768768z

But it will still execute with likely no extra time at all due to OOE and how fast arithmetics are.

OOE doesn't necessarily save you if you end up with a hard dependency on the value of the read (and even if it did, the little cores on ARM SoCs are in-order). This is a pretty obvious candidate for macro-op fusion, but I'm not sure whether this actually happens (and if it happens on ARM little cores, etc.)

Re: Why do arrays start at 0?

#674
post #553

Earlier quoted context omitted.

True, and it's a great example of how this whole drama is about a practical trade-off, not about a unique Right Answer. If you play piano, the second is the second finger; the fifth is the fifth finger, and it all makes sense. No problem. On the other hand trying to actually count that way (two thirds make a fifth and so forth) is maddening.

Clearly the solution is we need to start numbering fingers from 0.

No, just use subtraction for the intervals. Third finger minus first finger = 3 - 1 = 2.

The floors of a building might start at 1, but you go up 1 flight of stairs from the 5th to the 6th floor, not two.

Re: Why do arrays start at 0?

#675

Earlier quoted context omitted.

You do understand that besides thirds, fifths and sevenths, there really are seconds, fourths, sixths, ninths (same as second), elevenths (same as fourth) etc... as well, right? There even are intervals that are not named after a number e.g. the "tritone". The reason the 2nd and the 3rd note in a chord are called third and fifth is because usually chords are made with these intervals instead of dissonant intervals li…

It's that off-by-one nature of intervals that always bumps me. The difference between note a and b is (a-b+1). Calling an octave "an octave" feels to me like calling a numeric system with digits 0x0-0xf as "base 17"

By your logic, an octave/unison would be "seventh"/"zeroth"? (Note that "octave" literally means "eighth" in Latin.) I would think that could work too but the established terminology is not inconsistent. It's just 1-indexed. A lot of things are 1-indexed, in fact I think almost everything in spoken English is 1-indexed. A lot of mathematics, like number theory, is also 1-indexed. I think software engineers are a little too obsessed with 0-indexed things. I understand that things not being standard is annoying to us but pretending like this somehow makes music terminology broken is going too far.

I don't see how 0x0-0xf could be called base 17. 0xf is 15. Did you mean base 15? I think if mathematics terminology developed differently it could be called base 15. The same way binary is 0 and 1 but we call it base 2 because there are 2 digits, but we could totally call it base 1 too, who cares, it's all convention.

Re: Why do arrays start at 0?

#676
post #648

Earlier quoted context omitted.

In France, it is called the rez-de-chaussée. The “premier étage” (literally translated as “first floor”) is what the US calls the second floor.

Don't know French, but I wonder if the meaning of "etage" is similar to Polish "piętro", which literaly means something like "elevation". So, basically in Polish we have a specific word for ground level, and then we count how much elevated above the the ground the current level is. That's why "1st floor" is the one "elevated one level above the ground". In English you count "floors" and floor is a usable, hard surfac…

Interesting, thanks!

Etymologically, étage comes from the Greek στέγω (and gave the English word “stage”); it is a typically wooden cover. Since the first floor was often instead a continuation of the outside road (way back!), it was not considered a “stage”.

Re: Why do arrays start at 0?

#677

I started programming in assembly, then Fortran, Algol, COBOL, etc. 0 based index makes sense to me. The memory location is offset 0 bytes from where the first data byte is stored (without getting into endianism).

For me, it was 1 based index that makes sense to me.

I started programming in FORTRAN, then COBOL, then (Lord help me) RPG II. All had 1 based arrays. I got paid to program in RPG II eight hours a day, five days a week, for two and a half years.

A common bit of code was to create an array of month names. Then take an input date, say 20220825, and split that into $Year (first four characters) and $MonthNumber (next two) and $Day (next two).

$MonthIndex = strip the leading zero (if present) from $MonthNumber

Then, this code worked: $MonthName = @Month[$MonthIndex]

When I ran into my first language with 0 based arrays, and @Month[1] returned February, I was pretty much convinced you all had gone insane. December is an out of bounds condition now? Really?

Much later, a friend explained the stuff about multiplying the index by the array element length, and easily getting the memory location of the item in that array. It does make sense; but, I've always hated the code I've had to write that says $MonthName = @Month[$MonthNumber - 1]

Re: Why do arrays start at 0?

#678
post #573

Earlier quoted context omitted.

> Dijkstra's argument ( https://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF ) is that the index counts the number of predecessors. That's not an argument. It's a coincidence. There are applications where what you care about is the number of predecessors (indeed, that's what the compiler cares about, which is why we have 0-indexing in the first place), but they are a tiny minority of all indexing. > I think an age-…

> That's not an argument. It's a coincidence. There are applications where what you care about is the number of predecessors (indeed, that's what the compiler cares about, which is why we have 0-indexing in the first place), but they are a tiny minority of all indexing. I think there's nothing to say to the first two sentences but that I regard it as an argument that may be more or less convincing. I don't know exact…

> I think there's nothing to say to the first two sentences but that I regard it as an argument that may be more or less convincing. I don't know exactly what it means for something to be an argument vs. a coincidence; it is a coincidence that, say, my name is what it is, but it is nonetheless correct for me to argue that that is my name.

Sure. But we're talking about whether to index arrays from 0 or 1. It is true that naming an array element after the quantity of its predecessors will tell you the number of predecessors the element has. But that's not an argument for why you should do it; there would need to be some kind of benefit to having that information. Without a benefit, it's just something that happens to be true.

That's the difference between an argument and a coincidence.

Re: Why do arrays start at 0?

#679

Earlier quoted context omitted.

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…

Or alternatively: size_t i = length; while (i--) ...

Unless I am remembering C wrong, this would work but the

   for (size_t i = length; i-- > 0 ; ) ...
that several other people posted would not execute for index 0. Shouldn't it be this instead?

   for (size_t i = length; --i > 0 ; ) ...
Post reply on HN