Live data from Hacker News

Why do arrays start at 0?

buttondown.email

641–650 of 702 posts

Re: Why do arrays start at 0?

#641

Earlier quoted context omitted.

“machine code” isn't really the zero point that's special nowadays. The code the programmer wrote is compiled to something such as LLVM IR, LLVM IR is further compiled by LLVM to Assembly, this is further compiled by an assembler into machine code, and then the c.p.u. further compiles this to it's internal code as it executes it. “machine code” really is no more special in this chain of events than, say, LLVM IR.

Machine code is special because its the only thing the CPU can actually execute.

Except if the CPU is microcoded, those instructions might not mean what you think they do, and you need something like Intel's V-Tune to actually understand what the CPU is doing with them.

Re: Why do arrays start at 0?

#642
post #641

Earlier quoted context omitted.

Machine code is special because its the only thing the CPU can actually execute.

Except if the CPU is microcoded, those instructions might not mean what you think they do, and you need something like Intel's V-Tune to actually understand what the CPU is doing with them.

> ... to actually understand what the CPU is doing with them.

x86 has a developers manual for this purpose.

Re: Why do arrays start at 0?

#643

Earlier quoted context omitted.

Except 1-based indexing is what we use in normal language. We don't use "zeroeth" or "player (number) zero" etc. And the word "first" is shortened to 1st etc. Personally I think we'd be better off if programming languages stuck to the same convention - off-by-1 errors aren't the hardest problems to deal with but they're still annoying.

That's confusing ordinal and cardinal numbers. The element with index 0 is the first number. The element with index 1 is the second number, and so on. Using the term "zeroth" is basically some form of showing off (even though it's kinda fun), but will be utterly confusing when you get to the fifty-second element which is the last in a group of 53 elements.

I'm not confusing them, my point about abbreviating "first" as 1st was that in typical speech we start counting at 1. Nobody says "let's start with item zero on the list". But programmers are stuck with having to say/think "item 0 in the array".

Re: Why do arrays start at 0?

#644
Reminds me of a simple implementation of a Prisoners and Boxes riddle that showed up in my YouTube feed where I had to add a comment so others (and eventually future older me) will understand what I did.

>// if one prisoner fails all are dead (1st try -> tries = 0)

https://gist.github.com/amalic/c107b5289d946b758418778a95fdc...

That being said, it seems to be a junior software engineer issue where the brain isn't wired yet for "0-based" indexing. I don't hear such complaints from more experienced developers who have simply learned to appreciate it because like in my example above it is more intuitive to me.

I have added "and eventually future older me" above because I don't do much coding these days, and my career trajectory looks like it's going to be even less in the future.

Re: Why do arrays start at 0?

#645
post #545

Earlier quoted context omitted.

Yes, where I'm at — large country in Asia — we count floors starting from 0 (aka G for Ground). Confused me when I visited USA.

G or L (for lobby) followed by 1 isn't uncommon in the US. > Confused me when I visited USA It's applied inconsistently enough to be confusing for natives too!

I've just come back from the vacation in the US, and my experience was: when the hotel has G or L for the ground floor, it still has 2 for the floor directly above it. "G or L followed by 1" never happened to me.

Re: Why do arrays start at 0?

#646

Earlier quoted context omitted.

Except 1-based indexing is what we use in normal language. We don't use "zeroeth" or "player (number) zero" etc. And the word "first" is shortened to 1st etc. Personally I think we'd be better off if programming languages stuck to the same convention - off-by-1 errors aren't the hardest problems to deal with but they're still annoying.

> "player (number) zero" That would be because any game worth playing has at least one player... and so it's natural to continue from there. (In terms of language.)

You might have heard of Conway's Game of Life.

Re: Why do arrays start at 0?

#647
post #641

Earlier quoted context omitted.

Except if the CPU is microcoded, those instructions might not mean what you think they do, and you need something like Intel's V-Tune to actually understand what the CPU is doing with them.

> ... to actually understand what the CPU is doing with them. x86 has a developers manual for this purpose.

It has, and if you manage to know everything on those pages, for every single processor on the market, while taking into consideration motherboard architectures, and OS workloads you're our hero.

We are way beyond MS-DOS days and Michael Abrash's books.

Re: Why do arrays start at 0?

#648
post #56

Earlier quoted context omitted.

They do, it's called the ground floor.

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 surface on which you can put something, like a chair. That's why a floor on the ground level is treated the same as the floor above it - it is equally good on accommodating chairs, beds, and other stuff.

Re: Why do arrays start at 0?

#649

Earlier quoted context omitted.

That's confusing ordinal and cardinal numbers. The element with index 0 is the first number. The element with index 1 is the second number, and so on. Using the term "zeroth" is basically some form of showing off (even though it's kinda fun), but will be utterly confusing when you get to the fifty-second element which is the last in a group of 53 elements.

I'm not confusing them, my point about abbreviating "first" as 1st was that in typical speech we start counting at 1. Nobody says "let's start with item zero on the list". But programmers are stuck with having to say/think "item 0 in the array".

I don't disagree with you. I just don't think a programmer should be confused about the statement "item 0 is the 1'st item".

Re: Why do arrays start at 0?

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

If it is not on the hot path, it is likely free, but not guaranteed. If it is on the hot path then it is wasting a whole cycle. And of course in highly ALU-dependent code it is another instruction, so a fraction of a clock.
Post reply on HN