News for hackers. Stuff that matters.
Re:Why do arrays start at 0? by turboponyy on Wednesday August 24, 2022 @04:02PM (#32584991)
Nats start at 0, end of discussion - it's only logical to index by the naturals.
Re:Why do arrays start at 0? by mtizim on Wednesday August 24, 2022 @05:31PM (#32586051)
That's really silly - the natural numbers also famously start at either 0 or 1, depending on the country, the discipline, and the individual.
Re:Why do arrays start at 0? by thaumasiotes on Wednesday August 24, 2022 @06:04PM (#32586392)
I like to treat the naturals as starting at zero, but not for any mathematical reason. I just think that since it's easy to write ℤ+, it's more useful for ℕ to refer to something slightly different than it would be for ℕ to be a synonym for ℤ+ while anyone wanting to include zero in their set was forced to spell out "nonnegative integers".

A dichotomy between "ℤ+" on the one hand versus "ℕ" on the other is just plain more convenient than a dichotomy between "ℤ+" and "ℕ" on the one hand versus "ℤ\ℤ-" on the other.

Re:Why do arrays start at 0? by zzo38computer on Wednesday August 24, 2022 @06:47PM (#32586860)
It is because you mean two different things by "natural numbers", even though the same words are used for things that are not the same things.

I prefer to start by zero; I think it is more useful in general, and makes more sense mathematically for many (although not all) purposes, and systems that you will find objects and operations that have these properties too.

Re:Why do arrays start at 0? by topaz0 on Wednesday August 24, 2022 @04:17PM (#32585190)
If there were any justice in the world, they'd start at 2, as a handicap. They might even make it out of the bottom of the NL east if that was the case.
Re:Why do arrays start at 0? by JadeNB on Wednesday August 24, 2022 @04:06PM (#32585058)
> Nats start at 0, end of discussion - it's only logical to index by the naturals.

Now you'll just bring out the people who start the naturals at 1.

Re:Why do arrays start at 0? by ajg36 on Wednesday August 24, 2022 @06:39PM (#32586759)
Starting the naturals at 1 is quite onerous.
Re:Why do arrays start at 0? by colejohnson66 on Wednesday August 24, 2022 @07:17PM (#32587162)
That’s why I compromise and start at -1
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.
Re:Why do arrays start at 0? by thaumasiotes on Wednesday August 24, 2022 @06:09PM (#32586442)
> Nats start at 0, end of discussion - it's only logical to index by the naturals.

Well, no, obviously you want to index by ordinals, and they start at "first".

Re:Why do arrays start at 0? by salty_biscuits on Wednesday August 24, 2022 @04:43PM (#32585472)
Where 0 is the cardinality of the empty set, i.e. some empty collection. Not a convincing argument.
Re:Why do arrays start at 0? by JadeNB on Wednesday August 24, 2022 @05:11PM (#32585825)
> Where 0 is the cardinality of the empty set, i.e. some empty collection. Not a convincing argument.

Why not? "Every cardinality, except of the empty set, is a natural number" isn't very convincing to me, if we're making cardinal-based arguments.

Re:Why do arrays start at 0? by salty_biscuits on Wednesday August 24, 2022 @06:10PM (#32586458)
I'm not arguing that the natural numbers should start from one, rather that the usual path for developing the natural numbers starting with set theory is that zero is the size of a set with nothing in it. In maths they always talk about the first element in a vector, not the zeroth. It is a bad argument to point to mathematics for using zero based indices. It is not at all common to use zero based indexing there.
Re:Why do arrays start at 0? by JadeNB on Wednesday August 24, 2022 @07:07PM (#32587066)
I'm a mathematician, and 0-based indexing makes way more sense to me.

Dijkstra's argument (https://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF) is that we count the number of predecessors. This fits well with the mathematical usage, at least among mathematicians who care to dig into the order-theoretic foundations: the von Neumann construction of ordinals (and of cardinals as least ordinals of a fixed, well, cardinality), each finite ordinal is equal to its cardinality: 0 = \emptyset is a set with 0 elements, 1 = {0} is a set with 1 element, 2 = {0, 1} is a set with 2 elements, etc.

Re:Why do arrays start at 0? by salty_biscuits on Wednesday August 24, 2022 @07:59PM (#32587529)
I'm a mathematician too, and it doesn't make sense to me. It seems somewhat arbitrary depending on the application, i.e. I think it is a type problem. We use an Int to mean something special about array elements. I'd be happier with first() and last() methods really, I don't care for coming up with meanings based on implementation details.
Re:Why do arrays start at 0? by JadeNB on Wednesday August 24, 2022 @10:59PM (#32588914)
> I'm a mathematician too, and it doesn't make sense to me.

Just to be clear, I wasn't making some sort of bizarre appeal to authority; whether something makes sense is a matter of taste, not of mathematics, and can't be proven or dis-. I meant only to respond to:

> In maths they always talk about the first element in a vector, not the zeroth. It is a bad argument to point to mathematics for using zero based indices.

I don't know for sure which convention is more common, though I suspect you're right that it's the 1-based convention, but I do know that it's false that 'they'—meaning, among others, we!—always talk about the first element/component in a vector. I probably will do that in a first Linear Algebra class, because the textbook often does and I don't want to introduce unnecessary confusion; but usually in my own work, and sometimes when I teach upper-level math classes, I use 0-based indexing when it's necessary to choose.

Re:Why do arrays start at 0? by samatman on Wednesday August 24, 2022 @08:05PM (#32587582)
The question is whether 0 is a valid ordinal. If it is, why skip it?

If you allow 0 as a valid ordinal, it unifies ordination and measure. That's the best reason to do so.

There. Djikstra as haiku.

Re:Why do arrays start at 0? by thaumasiotes on Wednesday August 24, 2022 @06:11PM (#32586472)
It's not a convincing argument because you index into an array if you want to retrieve an element contained in the array. If 0 is the cardinality of an empty collection, it's not a valid index, because you can only index into non-empty collections.
Re:Why do arrays start at 0? by JadeNB on Wednesday August 24, 2022 @07:09PM (#32587085)
Dijkstra's argument (https://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF) is that the index counts the number of predecessors.

I think an age-based way to phrase it: in your 1st year, your age is 0; in your 2nd year, your age is 1; and so on. We can assign people numbers indicating what year of their lives they're in, or how many years they have lived, and both are fine, but we've settled on the latter.

Re:Why do arrays start at 0? by thaumasiotes on Wednesday August 24, 2022 @07:25PM (#32587237)
> 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-based way to phrase it: in your 1st year, your age is 0; in your 2nd year, your age is 1; and so on.

But that isn't even true. No one ever reports the age of their new child as 0; instead, they will report a positive number of months, or -- if it's an extremely new child -- of weeks or days.

Re:Why do arrays start at 0? by JadeNB on Wednesday August 24, 2022 @11:08PM (#32588972)
> 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 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.

These are all conventions anyway, and there is not much use (or even meaning) in arguing about which one is the right or wrong convention, just which one makes more or less sense; and this is one way to make sense of 0-based indexing, though of course there are also ways to make sense of 1-based indexing.

I'm not sure I buy that these cases are a tiny minority, but I'm certainly in no position to produce any data to the contrary.

> > I think an age-based way to phrase it: in your 1st year, your age is 0; in your 2nd year, your age is 1; and so on.

> But that isn't even true. No one ever reports the age of their new child as 0; instead, they will report a positive number of months, or -- if it's an extremely new child -- of weeks or days.

I can believe that ages aren't reported that way, although I think that a child who will be 1 year in 1 year should logically be said to be 0 years; but we can avoid that debate by considering future years: in, to pick the example that applies to me, my 42nd year of life, I am 41 (I could say 41 and 1 month, but I don't—in fact, there's a Seinfeld joke about that). Similarly, the 42nd entry in a 0-indexed array is indexed 41. It doesn't have to be that way, but I don't think one can argue that there's anything logically amiss about it (nor about 1-based indexing … but we do have to pick one).

Re:Why do arrays start at 0? by thaumasiotes on Thursday August 25, 2022 @03:58PM (#32599209)
> 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? by smugma on Wednesday August 24, 2022 @07:07PM (#32587056)
I learned them in elementary school (in the US) as starting at 1. Indeed, Google's first result for natural number is from Oxford Languages, and defines natural number as:

the positive integers (whole numbers) 1, 2, 3, etc., and sometimes zero as well.