Why do arrays start at 0?
501–510 of 702 posts
Re: Why do arrays start at 0?
#502I 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…
> To get the address of a particular item, this layout naturally leads to the formula "base address + index * element size", with "index" being 0-based. If you want to expose other indexing schemes in your language, you'll have to add more logic to convert the user-visible index back to 0-based before you can obtain the address. The easy way to do that is by shifting the base address. In pseudo-C (I think that comput…
It feels like you're introducing a million edge cases.
Re: Why do arrays start at 0?
#503Earlier quoted context omitted.
Not sure if assembly/machine code is that relevant. If one based indexing was more prevalent, the LEA instruction on x86 would just subtract one during execution
"just subtract one" would take a long time 50-60 years ago.
Re: Why do arrays start at 0?
#504Earlier quoted context omitted.
"just subtract one" would take a long time 50-60 years ago.
If you did this, you'd subtract one once at array creation. Or subtract whatever the offset is (really, you'd subtract 1 * data type size). Under the hood, using C syntax instead of assembly: int foo[n]; // what the user wrote // what happens behind the scenes, after a fashion int* foo = malloc(n * sizeof(int); // or sp - n * sizeof(int) if stack allocated; subtraction since stacks usually grow "down" foo = foo - 1;…
// 11 arrays are created here
int foo[10][10];
It doesn't make sense to create pointers for the 10 inner arrays, so the subtraction would presumably happen when referring to each inner array: int *z = foo[x]; // (char *)foo_sub_1 + (sizeof *foo)*x - (sizeof **foo)
foo[x][y]; // (char *)foo_sub_1 + (sizeof *foo)*x - (sizeof **foo) + (sizeof **foo)*y
In any case, this is more work, both for the computer and for the human. It's analogous to trying to figure out "which year of which century is X in?" (2022 is the 22nd year of the 21st century): year(x) = (x - 1)%100 + 1
century(x) = floor((x - 1)/100) + 1
The above formulae work for the 1-based convention, because they do the necessary 1 subtraction/addition in the right places. If we instead counted from 0, things would be a lot simpler (2021 would be year 21 of century 20): year(x) = x%100
century(x) = floor(x/100)
Unfortunately, the Romans started counting years before anyone knew about 0. I think this is basically why we have a convention of counting from 1: 0 simply wasn't discovered until relatively recently in human history. Earlier number systems such as Greek/Roman numerals don't have a way of representing it.Re: Why do arrays start at 0?
#505Earlier quoted context omitted.
> additional computational overhead at compile time, not runtime.
For constant addressing; for arr[i] = 2, you'll still need to subtract 1 from i with 1-based addressing when converting to machine instructions.
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+rax*8]Re: Why do arrays start at 0?
#506Re: Why do arrays start at 0?
#507Earlier quoted context omitted.
> It really comes down to a choice between a machine-focused (0) or human-focused (1) approach. Just think of 0-based as offset-based and 1-based as index-based. Both are intuitive just like that. I never get why people arguing over this bring pointers and memory (or anything computer related) to the table. No normal person is going to understand that, but everyone understands that if you don't move at all (0 offset)…
"Offset-based" is bringing in pointers; that's the thing it's an offset "from". "The beginning of the array" is just a pointer. I suppose saying that does have an advantage over explicitly talking about pointers, in that the word "pointer" is a piece of jargon that has a lot of baggage. That's just avoiding jargon, though, not really using a different model.
The advantages in measuring from origin can accrue to the person choosing to do it, because there are other reasons to do so which aren't satisfying the CPU.
Re: Why do arrays start at 0?
#508Earlier quoted context omitted.
Nats start at 0, end of discussion - it's only logical to index by the naturals.
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.
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?
#509Earlier quoted context omitted.
Quoted post unavailable.
Anecdotally, I taught my children numbers starting from zero with a similar routine, based on cardinality. They immediately got it. My kids' school also uses zero-based tables. "Natural" is a very loaded and ambiguous term, best avoided if you hope to have sane discussions.
I do not believe for a second that you taught them to count by holding up one finger for zero, two fingers for one etc.
Reminder that we're specifically talking about array indexing.
Re: Why do arrays start at 0?
#510Earlier quoted context omitted.
Nats start at 0, end of discussion - it's only logical to index by the naturals.
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.