Live data from Hacker News

Why do arrays start at 0?

buttondown.email

611–620 of 702 posts

Re: Why do arrays start at 0?

#611

Earlier quoted context omitted.

I would be fine with it all if 0-based was called "offset", with "index" reserved for 1-based. The element at offset 0 is the first element. Precision in naming is important.

> Precision in naming is important. It really isn't. The human race gets by without it the vast majority of the time.

The human race doesn't do science or engineering the vast majority of the time. When it does, precision in naming is important.

Re: Why do arrays start at 0?

#612
post #59

Arrays in Ada start at the index based on the index type of the array. You can even use an enumeration type as the index: type Day is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); type Hours is array (Day range ) of Natural; V : Hours (Monday .. Friday); Which index type you should use depends on the problem that you're trying to model. You can find out the lower/higher end of a type/variable with…

Wow, I never really looked at Ada code. Been using VHDL for the past 2 years a lot and when I looked at your code I was like: 'huh strange, this looks a lot like VHDL'.

Turns out both were invented by the DoD.

>Due to the Department of Defense requiring as much of the syntax as possible to be based on Ada, in order to avoid re-inventing concepts that had already been thoroughly tested in the development of Ada,[citation needed] VHDL borrows heavily from the Ada programming language in both concept and syntax. - https://en.wikipedia.org/wiki/VHDL

Maybe I should pick up Ada soon. That could be a fun journey! (I really love writing VHDL)

Re: Why do arrays start at 0?

#613
post #219
post #168

If you ask people which floor of building they're on, it's going to depend on which country they're in. In North America, at least, the first floor you walk into (in a sane city: I understand there are some which do not qualify in this respect due to hills or historic disaster recovery) is the first floor. On other continents, you enter the ground floor and need to take stairs or an elevating device to get to the fir…

The ground floor is 0. The floor above 0 is 1. The floor below 0 is -1. Anything else is crazy TBH, the whole point of integer numbers is to count things that start at a defined point and go opposite ways. Why shift it and then reinvent weird pseudo-negative numbers like S1? But then again people measure distance in feet and write dates as month day year :)

We have a building at the local university that has ground at one end level with transitory step between floors -1 and 0, you enter the building through the stair well, and descend or ascend to get to a floor. I wouldn't be surprised if an architect designed a building that did this on flat ground, violating the ability to number floors relative to ground with integers.

Re: Why do arrays start at 0?

#614

Earlier quoted context omitted.

Ah yes, the goes-to operator -->

And the wink operator, so the compiler knows you know the deal

I like how GP is called "operator-name" and instead of doing himself, he makes others joking with operator names. Although I'm not sure if it's altruism or highly manipulative behaviour.

Re: Why do arrays start at 0?

#615
post #536

Earlier quoted context omitted.

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…

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

Re: Why do arrays start at 0?

#616

Earlier quoted context omitted.

Yeah, but then couldn't the compiler just swap the 1 with a 0 and 256 with 255?

That gets really dicey if you're storing the value of the index in a variable. Does the programmer need to know that if it's an INDEX they're storing that goes from 1-256 they can fit that into an 8-bit value because the compiler will magic away that last bit? Speaking of which, how will the compiler know which 1's are really 0's and which 256's are really 255's? Will it compile that 1+1=1 if I use that result as an…

not really? It just changes how the compiler emits the x := *(addr + idx) operation where instead of doing something like

    ldr x, [addr, idx]
it just does

    sub t1, idx, #1
    ldr x, [addr, t1]
If we're talking about C, accessing invalid/OOB indices is undefined and so if idx happens to be zero and unsigned, we'll overflow and hit something unexpected, which is fine.

Re: Why do arrays start at 0?

#617
post #235

I don't quite understand the argument "0-based being easier for pointer arithmetic is nonsense because the language doesn't have pointers". Whether or not the language presents the concept of "pointer" to the user is independent of whether or not it uses pointers internally. And if it exposes arrays as a concept, it has to implement them somehow. The simplest possible implementation of arrays is having a start addres…

The "language" that has a 0-based indexing scheme is assembly. An HLL with 1-based counting that compiles to assembly will introduce additional computational overhead for the translation of the index. If 1-based indexing was used in assembly, then "mov 1(%ebx),%eax" would be the equivalent of "mov (%ebx),%eax".

> An HLL with 1-based counting that compiles to assembly will introduce additional computational overhead for the translation of the index

Not necessarily so for translation of an index that is evaluated at run time. PL1 (back in the 1960's) compiled arrays into 'dope vectors'. Each array's dope vector included highest and lowest valid subscripts for each dimension of the array and the address where the element with all zero subscripts would be found, if it existed.

Re: Why do arrays start at 0?

#618
post #3

Pascal's arrays start at 1, at least by default.

Pretty much all variants of BASIC have arrays starting at 1.

Except VB.NET which is only 0-based but you can just shut your eyes and imagine it's 1-based because you declare an array by specifying its upper bound which equals its number of elements in 1-based.

Re: Why do arrays start at 0?

#619

Earlier quoted context omitted.

> 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…

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

>and I believe RISC-V is similar

Yes it is. It was evaluated, carefully weighted and discarded, as it was not worth it.

Re: Why do arrays start at 0?

#620
post #605

Earlier quoted context omitted.

What do you consider "high-level code" and in what way does deeply understanding how a computer works influence your approach?

In college, I optimized the array removal routine from O(N^2) to O(N) by using INVARIANTS But it probably ran in like 100us anyway

Ugh, invariants...

Anyway, complexity stays relevant, no matter what language you use. That's the real deal.

Post reply on HN