Live data from Hacker News

Why do arrays start at 0?

buttondown.email

81–90 of 702 posts

Re: Why do arrays start at 0?

#81
post #6

I have always assumed that is just one less operation required to resolve the absolute memory address.

No. 1. Optimizing compilers exist. 2. Addressing at fixed offsets is cheap (a single instruction): https://en.wikipedia.org/wiki/Addressing_mode 0-based indexing is superior (as a default) because it simplifies a lot of common math, as discussed in TFA.

TFA = Thanks For Asking?

Re: Why do arrays start at 0?

#82

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.

If you took a standard ruler, a measurer of distance and which has literal index marks painted on it, and placed items along it, the item found at the head of the ruler would be found at the 0th index. You're quite right that we think of birth and its anniversaries in the same way.

Re: Why do arrays start at 0?

#83

It really comes down to a choice between a machine-focused (0) or human-focused (1) approach. The 0 makes a lot of sense in a C pointer world where memcpy and other alike functions can be written very thight. The 1 makes a lot of sense in a human world, when we count, we start at 1, we talk about the "1st", counting on finger starts with 1, etc. I once were at a Lua (1 indexed language) conference where this was disc…

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

And then the counting niceties come along: those perennial +1 mistakes are caused by the fact that, e.g. 4 and 5 are two numbers but they are only one apart.

Re: Why do arrays start at 0?

#84
post #67

This reminds me of arguments about male -> female and man -> woman (and human etc). Without offending anyone's delicate sensibilities - you may note that there is a short version and long version of those words, suggesting that the longer version is derivative of the short version. However, when you examine the origin of the words, that is not the derivation. The derivation and path to English is quite complicated. Y…

I really fail to understand what the etymology of sex and gender classifications has to do with array based indexing.

Re: Why do arrays start at 0?

#85

It really comes down to a choice between a machine-focused (0) or human-focused (1) approach. The 0 makes a lot of sense in a C pointer world where memcpy and other alike functions can be written very thight. The 1 makes a lot of sense in a human world, when we count, we start at 1, we talk about the "1st", counting on finger starts with 1, etc. I once were at a Lua (1 indexed language) conference where this was disc…

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

Rulers too. A ruler starts at 0, not 1.

Re: Why do arrays start at 0?

#86
Array indices denote offsets from the base address. The first element is at the base address of the array, hence the offset is zero. It avoids a conditional when resolving the address, introducing it would slow down all forms of memory access across the language domain.

Re: Why do arrays start at 0?

#87

It really comes down to a choice between a machine-focused (0) or human-focused (1) approach. The 0 makes a lot of sense in a C pointer world where memcpy and other alike functions can be written very thight. The 1 makes a lot of sense in a human world, when we count, we start at 1, we talk about the "1st", counting on finger starts with 1, etc. I once were at a Lua (1 indexed language) conference where this was disc…

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

Birthdays are anniversaries. Anniversaries are annual activities when we celebrate/honor past events. They do not include the event itself. The first anniversary is 1 year after the initial event.

Re: Why do arrays start at 0?

#88
post #66
post #48

Indexes start at 1. Offsets start at 0. Arrays in programming use offsets. for(int i=0;i The error here is calling it 'i' for index. It should be 'o' for offset.

Arrays in the programming languages you’re familiar with use offsets. Not all do [1]. 1. https://therenegadecoder.com/code/which-programming-language...

Of course, and I've used Lua for a long time.

The point is that offsets and indexes have different meaning.

Re: Why do arrays start at 0?

#89
post #84
post #67

This reminds me of arguments about male -> female and man -> woman (and human etc). Without offending anyone's delicate sensibilities - you may note that there is a short version and long version of those words, suggesting that the longer version is derivative of the short version. However, when you examine the origin of the words, that is not the derivation. The derivation and path to English is quite complicated. Y…

I really fail to understand what the etymology of sex and gender classifications has to do with array based indexing.

> "Bringing it back to the topic at hand - "Why we use 0" - because it works. There are many times when it has been evaluated, and at least to the people who design languages, it is more mentally appealing."

The linked post is not just about a technical decision, it's about a social history.

The conclusion of the linked post:

> "Lessons

Things can have more than one cause. Don’t trust easy explanations.

Always look for information that refutes your theory, not just information that supports it. Hoye stopped as soon as he had a satisfying answer and didn’t keep researching.

Don’t be a dick.

It is tragically easy to trick me into doing free research."

Post reply on HN