Live data from Hacker News

Why do arrays start at 0?

buttondown.email

291–300 of 702 posts

Re: Why do arrays start at 0?

#291
post #252

Earlier quoted context omitted.

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.

Nope. Is the empty set countable? (Yes.) Dictionary: nat·u·ral num·bers the positive integers (whole numbers) 1, 2, 3, etc., and sometimes zero as well Countable: https://en.wikipedia.org/wiki/Countable_set Set theory: https://en.wikipedia.org/wiki/Ordinal_number

From your own link on countable sets:

> Equivalently, a set S is countable if there exists an injective function f : S → N from S to N; it simply means that every element in S corresponds to a different element in N.

Defining N is usually done via a successor set, on which case 0 makes no sense to include.

Re: Why do arrays start at 0?

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

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.

Re: Why do arrays start at 0?

#293
post #10

Earlier quoted context omitted.

This is primarily the actual answer. While most languages force you to see the array as an array, in C/C++ (in C++ I'm only referring to the memory allocated variable type, not an "array class") you can see it as a pointer and do your own math to address any part of it that you want. And the calculation for that memory address is idx * sizeof(the thing in your array). So the real answer here is that array semantics n…

There's no reason why memory addresses had to start at 0.

[deleted]

Re: Why do arrays start at 0?

#294

Earlier quoted context omitted.

As Jens Gustedt points out[1], the following intentional unsigned overflow works perfectly for downwards iteration (even when length is 0 or SIZE_MAX), though it looks a bit confusing at first: for (size_t i = length - 1; i You are also free to start at any other (not necessarily in-bounds) index, just like with ascending iteration. [1] https://gustedt.wordpress.com/2013/07/15/a-praise-of-size_t-...

Uh I'm confused but don't know c++. why doesn't that loop end instantly? I mean length - 1 Or does it only terminate when the number underflows? Terribly confused here

For loops are translatable from:

  for(initialize; condition; increment) { ... }
to:

  initialize;
  while(condition) {
    ...
    increment
  }
(more or less, some scoping things not encompassed by the above; this is also how pretty much every for loop in a C-syntax language works) The condition of a for loop is equivalent to a while loop's condition. So yes, length - 1 will be true on the first iteration, which is fine because the loop continues as long as that condition is true.

What the above approach takes advantage of is that when underflow eventually happens you'll have this condition:

  MAXINT 
Which will terminate it for all possible values of length.

Re: Why do arrays start at 0?

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

0 for offsets, 1 for ordinals, that's usually the rule. Of course, both are conventions.

Re: Why do arrays start at 0?

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

What city are you in, out of curiosity?

Re: Why do arrays start at 0?

#297
post #247

Earlier quoted context omitted.

A rare case where 1-based indexing is more convenient is complete binary trees laid out breadth-first (as in a standard binary heap): parent is i div 2 and children are 2i and 2i+1 when starting at one and who knows what when starting at zero. But that’s the only one I know.

With 0-based indexing the children are at 2i+1 and 2i+2. The parent is at (i-1) div 2. Not hard to figure out.

> With 0-based indexing the children are at 2i+1 and 2i+2. The parent is at (i-1) div 2.

> Not hard to figure out.

While that's true, "you just shift by 1" is equally good at all arguments for or against 0-based indexing, so deploying it here probably won't convince.

Re: Why do arrays start at 0?

#298
Simple answer: because everything in computers starts at 0. Address 0 is the first byte of memory, etc.

You can now ask, "why does everything start with 0"? I guess because it gives you nice round (base-2) numbers. With 4 bits you can have 16 different "numbers", either 0 to 15 or 1 to 16. However, representing the number "16" in binary requires 5 bits, so you need an additional bit for the same number of elements. So representing 4 bits as 0 to 15 makes more sense

Re: Why do arrays start at 0?

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

When I was at Leeds University the ground floor of the Physics Admin building was Level 6 (it went up to Level 11). It was built on a hill and according to legend this allowed room for expansion (which never happened) down the hill without requiring negative floor numbers.

To add to the fun the only continuous corridor through the entire T-shaped building was on Level 10, one end of which (the base of the T) was at ground level on its part of the hill. There was a famous 'kink' in the middle of the top / cross-bar of the T (apparently the longest corridor in Europe) because they started building from each end and were slightly off.

Post reply on HN