Live data from Hacker News

Why do arrays start at 0?

buttondown.email

441–450 of 702 posts

Re: Why do arrays start at 0?

#441
post #382

Earlier quoted context omitted.

Where 0 is the cardinality of the empty set, i.e. some empty collection. Not a convincing argument.

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

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?

#442
post #382

Earlier quoted context omitted.

Where 0 is the cardinality of the empty set, i.e. some empty collection. Not a convincing argument.

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

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?

#443

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…

> human-focused (1) approach. TBH humans would have been better of if we were 0-based, it's just a convention. And we have the confusing language where "20th century" means 1900's. If we wanted to bring the 1-indexing we use in language to the fullest extent here to fix that particular issue, time counting would have to start at 1111. Except that won't work once reaching 5-digit years. If we would start with "zeroeth…

> where "20th century" means 1900's

to this day I can't seem to be able to explain to people that Jan 1 2000 was NOT the start of the new millennium, but rather Jan 1 2001

https://www.latimes.com/archives/la-xpm-2000-dec-26-mn-4810-...

Re: Why do arrays start at 0?

#444

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

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;
All references into foo will now work just fine so long as they are within the [1,n] range (same issues as with 0-based there since C doesn't carry size information for checking array bounds access). This adds one extra instruction per allocation (which includes allocation on the stack) for all non-0 offsets, but then all access will have the same cost whether 0-based, 1-based, or arbitrary-based. That's a non-zero cost, but it's not exorbitant since you'll be accessing much more often than allocating (and if it's reversed, something weird is happening).

Re: Why do arrays start at 0?

#445
post #314

Earlier quoted context omitted.

Waste 0th element or reuse it for something like length (hello, pascal strings). Another option is using base_address - element_size as your array value. Another option is using +element_size for all array accesses, assembly languages usually have this instruction. There’re many options to use 1-based indexing without sacrificing performance.

> assembly languages usually have this instruction Is that so? I wasn't aware of that.

At least x86_64 can put something like `ptr[index * 4 + 4]` in one instruction (assuming that variable values are in registers). Not completely sure about ARM.

Re: Why do arrays start at 0?

#446
post #323

Earlier quoted context omitted.

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.

Can't say I've really thought this through, but couldn't you just subtract 1 (*sizeof(X)) from the arr address?

What would memset(arr, 0, sizeof(arr)) do in that case? I can see myriad problems with having arr itself point outside the memory range allocated to arr[min..max].

Re: Why do arrays start at 0?

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

I've been living in North America for decades. Every building has G (zero). This is the floor that you walk into and has a lobby. The next floor up is 1 as such is labeled in the elevator. So your initial assumption is incorrect from my experience.

This is not my experience in the US. Most places either have G or 1 for the first floor. Then 2 for the next. So some elevators say G, then 2, 3, and so on.

I've seen setups like you mention, but they definitely aren't the majority in places I've lived.

Re: Why do arrays start at 0?

#448

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. Your age is 1-indexed. It's a 'birthday' in English and German ('geburstag'); in French it's 'anniversaire', and so on. But pretty much everyone indexes age from 1. The fact of your birth is the transition from (legal) non-existence to existence, the equivalent of a dimensionless point.

Age is definitely 0-indexed - a newborn and exactly 1 year old differ by a year. People also count months during the first year, which is still 0 years old. As in 00:xx is the first hour, and 01:xx is the second. If you're 30 years old, it's your 31st year of life now.

But gregorian epoch itself is 1-based. 1AD (0001-01-01) goes right after 1BC (-0001-12-31). There was no 0000-mm-dd. That's why 3rd "millenium" and 21st century started at 2001-01-01 and not at 2000-01-01. YYYY means not how many whole years already passed, but which incomplete year goes right now. On the other hand, your age means "whole years passed since birth [plus maybe a few months]".

Re: Why do arrays start at 0?

#449

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

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

Re: Why do arrays start at 0?

#450

Earlier quoted context omitted.

That would give a sequence of N+1 elements, though. Confusing if you had a function like range(N), for example.

Why, it would be an inclusive instead of exclusive range. N is the last element, so no reason for N+1.

If we use inclusive ranges where 0 ≤ i ≤ N, then len(range(N)) == N+1.

e.g. len(range(3)) == len([0, 1, 2, 3]) == 4

Post reply on HN