Live data from Hacker News

Why do arrays start at 0?

buttondown.email

521–530 of 702 posts

Re: Why do arrays start at 0?

#521

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

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?

#522

Earlier quoted context omitted.

The moment you are born you are 0 years old (or perhaps 0.75 years old, but we don't usually recognize that). We count from zero in this case, at least implicitly. In some cultures you are considered 1 the moment you are born, so the zero indexing isn't universal here, but typical in North America as noted earlier.

No, we don't count from 0. That's like saying we start counting a baker's cup from 0 because you can have half a cup.

Yes this is correct!

The milliliters in a baker's cup are a scale based on 0!

Re: Why do arrays start at 0?

#523

Earlier quoted context omitted.

What do you do for a living, and why are they paying you not to understand this?

Do you know the definition of countable? A set S is countable if there is a one-to-one mapping from S to N where N is the natural numbers. Do you know that 0 is not a member of the natural numbers? We literally start counting at 1 by definition of countable.

Are you a time traveller from the late 17th century?

Re: Why do arrays start at 0?

#524

Earlier quoted context omitted.

Yes, exactly. One of which carries an implicit zero indexing. The date of birth doesn't disappear just because you decided to use a different measuring device.

No, it doesn’t carry an implicit 0 index. Measuring age (in the West) is like measuring distance. You start at 0, but that doesn’t mean the first item is at index 0.

Birthdays are literally indexed by the moment of birth as zero.

You can't possibly be serious.

Re: Why do arrays start at 0?

#526

Earlier quoted context omitted.

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.

No, old calendar systems are one indexed. In those system, there is literally no year zero; the first year is year one. This leads to crazy things like year 100 being part of the "first century" and year 101 being part of the "second century". That is not the case with age or birthdays which are, thankfully, zero indexed. The first year of human life is age=0, birthdays=0.

So that we don't create more falsehoods programmers believe about dates, ISO has defined decades and centuries as starting with 0, not 1.

So as far as your job is concerned, that's when they start. Hope that helps.

Re: Why do arrays start at 0?

#527
Because back when computers were young, all bit representations were needed to represent something and having 000 not be used as a valid value wastes a huge percentage of available space.

Re: Why do arrays start at 0?

#528

Earlier quoted context omitted.

would it? or would it be just a differently wired circuit?

the "differently wired circuit" would be an extra stage of logic computing a carry all the way from lsb to msb (an ALU outside the ALU?) and would contribute a fair bit of extra time. Easier to just use the ALU to do it, which is inserting an extra instruction, also a time waster.

If you're already doing base index addressing (eg, [SI + DI]) you're already using the ALU to compute your memory address, and it's not _that_ much extra wiring to have the ALU support either a constant power-of-two displacement or even a three-way addition; a constant displacement for a basic ripple adder is a XOR and an AND NOT, eg. The x86 family does three-way addition (and a shift) to support "[ESI] [EBX * scale + displacement]".

Re: Why do arrays start at 0?

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

[deleted]

Re: Why do arrays start at 0?

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

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.

Defining index as the element wise offset is a precise well formed definition/name.

It's also a definition commonly used in math, through not always. It depends a lot of the area of mathematics and even the cultural context.

The only reason we sometimes feel 0-index is wrong IMHO is because the english language describes entities in a sequence as 1st, 2nd, 3rd etc.

But I wouldn't be surprised if there is some human language somewhere which does it differently. Like something which roughly matches to english like "head, head+1, head+2, ...".

Post reply on HN