Live data from Hacker News

Why do arrays start at 0?

buttondown.email

451–460 of 702 posts

Re: Why do arrays start at 0?

#451

Earlier quoted context omitted.

Oddly(?) though, most parents of young children don't refer to a baby as being "zero years old." Rather, we break them down into smaller units: two days old, six weeks old, four months old.

It helps that change happens particularly fast at that age, so the difference between newborn and 6 months is worth mentioning. Later on the units go up again and we just say "I'm in my 30s" :P On the other hand a computer/car/house can also be 10 years old but not 0.

On the other hand a computer/car/house can also be 10 years old but not 0

Really? When I buy a new PC, to me it will get 1 year old only after a year. Before that it is just NEW. Is that what you meant?

Re: Why do arrays start at 0?

#454
post #339

Zero-based indexing is always wrong; an ordered collection of things does not have a zeroth thing. Sometimes you have a data structure called an "array", which means "a bunch of things that are the same size next to each other in memory". You can store the address of the whole array as the address of its first element, and offset into it by an offset; clearly, the offset of the first element is zero, but it's not the…

An ordered collection of things starts no things from the beginning.

A journey starts 0 metres from the origin.

If you count the number of _ you are along your journey and start with 1, then at the second metre you are at the 101st centimetre.

When someone says how many dragons have you slain? Just as you leave and haven't heard of any dragons yet, you don't say "I'm slaying my first dragon now"

Every construction of the natural numbers I have seen starts with 0.

The first hour of the day has passed when the clock strikes one. The hour preceding that is the same day. English calls it 12 because it was invented before we had a firm grasp of zero and english is inconsistent. 24 hour clocks call it 00.

1 based indexing can be correct, but 1 based ordered discrete sets map incredibly poorly to any ordered set with a different number of elements (such as the reals). Additionally dealing with ranges is less easy to make consistent.

0 based indexing is always correct.

Re: Why do arrays start at 0?

#455

Earlier quoted context omitted.

> when we count, we start at 1, we talk about the "1st" Although often with an implicit zero. Under typical North American culture, your 1st birthday, for example, is more accurately the first anniversary of your birthday. Your birth is zero indexed.

No, birthdays are 1 indexed. Your Nth birthday is the day you turn N years old. When you're 1 year old, you've been alive for 1 year, not 2 years.

Haha, this thing is messed up. Your "first birthday" is technically second, because the first was at your, well, day of birth. But we people love to complicate things and count 1st birthday as a number of annual celebration events after the first mm-dd of birth. Off by one as it is.

Re: Why do arrays start at 0?

#456
post #292

Earlier quoted context omitted.

Nats start at 0, end of discussion - it's only logical to index by the naturals.

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

Starting the naturals at 1 is quite onerous.

Re: Why do arrays start at 0?

#457
post #339

Zero-based indexing is always wrong; an ordered collection of things does not have a zeroth thing. Sometimes you have a data structure called an "array", which means "a bunch of things that are the same size next to each other in memory". You can store the address of the whole array as the address of its first element, and offset into it by an offset; clearly, the offset of the first element is zero, but it's not the…

Some guitars have a zeroth fret; an actual fret at the nut.

Re: Why do arrays start at 0?

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

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

Should work 99.99999% of the time until someone tries to put an array at 0x000001 :)

Re: Why do arrays start at 0?

#460
I think that zero indexed is generally better, including that it involves adding the index number to the base address, and other properties that can be helpful.

However, it can also be useful for many purposes to allow arbitrary ranges, so a programming language probably should allow that; if the index of the first element is not zero, then the base address will not be the address of the first element (if the first element index is positive, then the base address may actually point into a different array, or a different variable). Then, if you have the base address and add the index, you will have the proper address of that element, whether or not the first index number is zero.

Zero is not always the most useful starting index; sometimes other numbers (which may be positive or negative) are useful. But, I think in general, zero is better.

Post reply on HN