Live data from Hacker News

Why do arrays start at 0?

buttondown.email

691–700 of 702 posts

Re: Why do arrays start at 0?

#691
post #607

Earlier quoted context omitted.

An array of length 0 starts at 0 and ends with 0

> An array of length 0 starts at 0 and ends with 0 Nope. The moment you "start" somewhere you occupy 1 unit of memory. Thus no longer an "array of length 0". There is no such thing as an array of length 0. It absolutely does not exist. You cannot write source code to represent it. ie assuming we are talking about the pure data structure of an array. Some languages may have some abstraction built on of arrays (ie c-st…

C++ requires every value to take at least one byte of memory, but this isn’t true of all languages. Additionally, C++20 adds [[no_unique_address]], which allows zero-sizes types.

iopq points out Rust as a language that does allow zero length arrays in a sibling comment.

While ISO C does not allow zero length arrays, GNU C does. There’s also flexible array members, which can be length 0.

Re: Why do arrays start at 0?

#692
post #544

Earlier quoted context omitted.

Defining index as the element wise offset is a precise well formed definition/name. It's also a definition commonly used in math, through not always. It depends a lot of the area of mathematics and even the cultural context. The only reason we sometimes feel 0-index is wrong IMHO is because the english language describes entities in a sequence as 1st, 2nd, 3rd etc. But I wouldn't be surprised if there is some human l…

> Defining index as the element wise offset is a precise well formed definition/name. But it's less precise than offset and obviously introduces confusion. Whoever originated the term "0-indexed" should have just suggested we use a better word rather than keeping the inaccurate word (proven by the fact you're currently looking for a way to remove some confusion around the currently chosen word) and prefixing it with…

you can say the same for 1-indexed, because from the get to go both existed.

Also it's not less precise, as there are many different kinds of offsets too. First you have element wise and byte wise and similar offsets, then offsets are inherently relative to something, and that's not always the first element. But could be an offset from the back and I have even seen a offset from one element before the first element in some very unusual use-cases.

Re: Why do arrays start at 0?

#693

Earlier quoted context omitted.

> An array of length 0 starts at 0 and ends with 0 Nope. The moment you "start" somewhere you occupy 1 unit of memory. Thus no longer an "array of length 0". There is no such thing as an array of length 0. It absolutely does not exist. You cannot write source code to represent it. ie assuming we are talking about the pure data structure of an array. Some languages may have some abstraction built on of arrays (ie c-st…

C++ requires every value to take at least one byte of memory, but this isn’t true of all languages. Additionally, C++20 adds [[no_unique_address]], which allows zero-sizes types. iopq points out Rust as a language that does allow zero length arrays in a sibling comment. While ISO C does not allow zero length arrays, GNU C does. There’s also flexible array members, which can be length 0.

I'd say this transcends language.

Sure a language can define some abstraction that says "hey I'm length-0 array!". But this is more like a statement of "hey I don't exist yet, but if i do exist in the future I'll be of type array and at least length-1".

You can't access index 0 of a length-0 array.

You can only access index 0 of length-1+ array.

Re: Why do arrays start at 0?

#694
post #688

Earlier quoted context omitted.

> An array of length 0 starts at 0 and ends with 0 Nope. The moment you "start" somewhere you occupy 1 unit of memory. Thus no longer an "array of length 0". There is no such thing as an array of length 0. It absolutely does not exist. You cannot write source code to represent it. ie assuming we are talking about the pure data structure of an array. Some languages may have some abstraction built on of arrays (ie c-st…

Allowed in Rust: https://play.rust-lang.org/?version=stable&mode=debug&editio... it's an array of length 0, but it can't be "used" because it has a length of 0 so it occupies no RAM

> An array of length 0 starts at 0 and ends with 0

But such an array does not "start" at 0 as it does not exist. You need an array of at least length-1 to "start" at 0.

Re: Why do arrays start at 0?

#695
post #685
post #660

Earlier quoted context omitted.

> But you're talking about adding one instruction to every single data access. No, you misunderstood. You subtract one from the pointer at allocation time. You don’t add an instruction every time you access.

And even if it was an instruction for every access, memory access is much more expensive than and add (at least nowadays -- I don't know if that's always been true).

Similarly I like to think about it as the add/subtract instruction is very small compared to malloc (and maybe could be included in a custom allocator for free). So the one extra instruction will always be somewhere between relatively minor to negligible and unnoticed.

Yeah today memory access on a cache miss is really expensive. I think memory was normally running at a different clock rate than the CPU even 50-60 years ago, so has always been something where you can’t touch memory every instruction. The latency of a cache miss wasn’t nearly as bad in the past. When the data cache was invented, a main memory access was like 4 times slower than register access. Today, main memory access can be more than a hundred time slower, sometimes it can approach a thousand times slower, which is why we always have multi-level caches now. And memory latency is still getting worse. If someone figured out how to make memory faster without increasing the cost or the energy consumption, they’d be rich! ;)

Re: Why do arrays start at 0?

#696
post #684

Earlier quoted context omitted.

The romans weren't the only people who knew how to count back then... And how many of the other (presumably arbitrary) choices for smallest number were zero? And why did it take over one thousand years to come up with ordinal "zeroth" after we knew about cardinal "zero"? > It seems that when labeling ... number ... that precede it, plus one. I don't think so. It's the number that you have counted once you've counted…

How do you write zero in Roman numerals? Answer: you can't. Even though they used their number system for adding amounts of money, they hadn't figured out "cardinal zero" yet. It was introduced into western mathematics through Fibonacci in the 1200s at the same time that Hindu-Arabic numerals were adopted, which use "0" as a placeholder (compare this to earlier Greek numerals which work similarly to the system we use…

That's what I'm talking about (though I guess the "thousand years" estimate was slightly high). Why did it take hundreds of years after the introduction of zero to get "the zeroth element of a sequence", which is quite a new usage, and even now is confined to specialized fields?

Re: Why do arrays start at 0?

#697

Earlier quoted context omitted.

> "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.

Fair :)

Re: Why do arrays start at 0?

#698
post #536

Earlier quoted context omitted.

Defining index as the element wise offset is a precise well formed definition/name. It's also a definition commonly used in math, through not always. It depends a lot of the area of mathematics and even the cultural context. The only reason we sometimes feel 0-index is wrong IMHO is because the english language describes entities in a sequence as 1st, 2nd, 3rd etc. But I wouldn't be surprised if there is some human l…

Which floor is the first floor in your building? Depends which country your building is in, and more! My apartment building is 1,2… but my mall is G,M,1,2… (same country different architects). For many years I lived in Europe where it’s usually G,1,2… (where G can also be E or Fsz or whatever) and when I was in the US I had to remember that 1 is G, though L,2,3… is also common. City people live with index ambiguity a…

> Which floor is the first floor in your building?

0.5 ... yep that won't work

In some buildings build around 1900 where I live the "ground" floor is not on the ground but around 1/2 a floor above the ground floor. While the cellar is just half below the ground. Because of this the cellar has above ground level windows allowing legal cellar apartments (oversimplified in Germany apartments require windows which are above ground).

This creates a lot of confusion.

By convention this 0.5 floor still counts as ground floor with the floor number 0. But people confuse it all the time for the first floor. Especially if in some cases you do have a few utility rooms exactly at ground floor.

Now things get worse if you live in a cellar apartment in such house: By convention floor -1.

Because nearly everyone will thing you mistyped as normally you legally can't have apartments below the ground floor.

Another fun index ambiguity is for apartments build on a mountain side/cliff.

In this case depending on which side of the house you look at the ground floor might be at, above or below the ground level. Often the floor level with the main entrance counts as ground floor, but sometimes it is not the case and e.g. the lowest floor not below ground or the highest floor even with a "ground".

So yes, index/floor level ambiguity is everywhere and much worse then just is ground floor 0 or 1 ;)

Re: Why do arrays start at 0?

#699
post #651

Earlier quoted context omitted.

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

It's slightly different - machine code is special because it's a visible API between our programming work and the CPU sillicon. Microcode is private implementation detail which you usually can't affect as a software developer. Machine code is something you are directly creating by writing things though.

Why is that visible a.p.i. different than, say, LLVM i.r. which is also a visible a.p.i.?

Why is the c.p.u. special?

Re: Why do arrays start at 0?

#700

Earlier quoted context omitted.

It’s a condition to run, not a condition to stop

That was my reaction - why should anyone think it might be the latter? Are there languages that do have such a syntax without explicit keywords ("do...until")?

I think lisp or scheme does. I was often confused by that when I was playing with it
Post reply on HN