News for hackers. Stuff that matters.
Re:Why do arrays start at 0? by lttlrck on Wednesday August 24, 2022 @07:31PM (#32587287)
other important and day to day tools like rulers, clocks and speedometers also start at zero. So it's not exactly "bending humans to microprocessors weird alien ways".

It all makes perfect sense in the context of measuring.

Re:Why do arrays start at 0? by foobarbaz33 on Wednesday August 24, 2022 @08:23PM (#32587730)
The analogy is slightly off though. The first position of an array occupies 1 unit of memory. 0 is not a possible measurement in the world of arrays.

Maybe a null terminated string could have length 0 if you don't count the terminator. But that 0 is a property of the "string" abstraction. The actual "array" would still be length 1.

Re:Why do arrays start at 0? by iopq on Thursday August 25, 2022 @02:23AM (#32590263)
An array of length 0 starts at 0 and ends with 0
Re:Why do arrays start at 0? by foobarbaz33 on Thursday August 25, 2022 @09:33AM (#32593489)
> 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-strings) that can have length 0 but these are not "arrays" and if they are defined they still occupy 1 unit of memory for the terminator.

Re:Why do arrays start at 0? by steveklabnik on Friday August 26, 2022 @07:50AM (#32606125)
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? by foobarbaz33 on Friday August 26, 2022 @08:39AM (#32606623)
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? by iopq on Friday August 26, 2022 @05:15AM (#32604958)
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

Re:Why do arrays start at 0? by foobarbaz33 on Friday August 26, 2022 @08:43AM (#32606655)
> 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? by iopq on Wednesday September 07, 2022 @02:21PM (#32754203)
I can write a loop like this:

`for(int i=0; i < 0; ++i)` and it will run 0 times, the exact amount of times I expected

Re:Why do arrays start at 0? by foobarbaz33 on Saturday September 10, 2022 @10:04AM (#32790751)
But again your not starting at "index 0" of an array that exists.

This loop is not processing any physically existing array. In fact the compiler would remove this block of code as it is a no-op.

Re:Why do arrays start at 0? by topaz0 on Thursday August 25, 2022 @09:16PM (#32602189)
The programming languages that I use most often can all represent empty arrays just fine. Of course, indexing into them is an error. But defining them is commonplace, and not being able to define them would make a lot of my code a lot more complicated.
Re:Why do arrays start at 0? by chillfox on Wednesday August 24, 2022 @08:41PM (#32587855)
With those other things zero is zero, but with arrays the zero index is the first item. The disconnect really starts when you get the size of an array and then have to minus it by one to have the last index.

To take your ruler example and apply array logic to to. Say you are measuring a 2cm thing. If the ruler follows the index then it would show 1cm (the last index), and so you would have to add + to get the size/length.

Having the index not align with the size is a pain that results in a lot of +/-1 code that a compiler could just as easily have handled.

Re:Why do arrays start at 0? by fsckboy on Wednesday August 24, 2022 @10:32PM (#32588719)
it's a good idea to call the 0th element of an array the zeroth element, else you encourage off-by-one errors. The size of the array, the "count", is that which your index must be less than.

I'm not saying it has to be this way and it can't start with 1, I'm saying what you are pointing out as a flaw is actually the mixing of systems.

in computer programming teams it's best to adopt a standard unambiguous language to communicate to one another so as not have to constantly say "do you mean...?"

Re:Why do arrays start at 0? by iopq on Thursday August 25, 2022 @02:27AM (#32590285)
No, it's the first element, if it's a 32 bit int it still occupies bytes

0..4

the second int occupies bytes

4..8

you can see the array up to the second int occupies the contiguous space 0..8 which 0..4*2 and it has TWO elements

Re:Why do arrays start at 0? by topaz0 on Wednesday August 24, 2022 @08:24PM (#32587737)
Rulers, clocks, and speedometers measure continuous quantities, i.e. ℝ. The natural numbers are irrelevant there.