Live data from Hacker News

Why do arrays start at 0?

buttondown.email

281–290 of 702 posts

Re: Why do arrays start at 0?

#281
post #95

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.

That's why I prefer the Superior(TM) Korean age counting. You are one year old when you're born (it's your first year!). You are two year old on the next New Year's day. (Congratulations, it's your second your now!) So, if you're born on December 31st, you're two years old the next day. (I see no problem, but apparently some people are hung up on such minor details. I can't fathom why.)

> I see no problem, but apparently some people are hung up on such minor details. I can't fathom why.

Americans and their alcohol laws…

Re: Why do arrays start at 0?

#282

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

It’s a condition to run, not a condition to stop

Re: Why do arrays start at 0?

#283

Because otherwise you would be wasting a perfectly good number for no reason, which means you need to use more bits to do the same thing. To write 4 numbers (including zero) you only need two bits 0: 00 1: 01 2: 10 3: 11 To write 4 numbers if you avoid using the number zero, you need three bits 1: 001 2: 010 3: 011 4: 100 If you extrapolate that a little bit, you'll realize that you'll need two bytes (1 Byte + 1 bit…

The same applies to counting in other bases too. For instance, in 1-indexed counting grids for kids, the last column always feels out of place.

0-indexed decimal grid:

   0  1  2  3  4  5  6  7  8  9
  10 11 12 13 14 15 16 17 18 19
  20 21 22 23 24 25 26 27 28 29
  30 31 32 33 34 35 36 37 38 39
  40 41 42 43 44 45 46 47 48 49
  50 51 52 53 54 55 56 57 58 59
  60 61 62 63 64 65 66 67 68 69
  70 71 72 73 74 75 76 77 78 79
  80 81 82 83 84 85 86 87 88 89
  90 91 92 93 94 95 96 97 98 99
1-indexed decimal grid:

  1  2  3  4  5  6  7  8  9  10
 11 12 13 14 15 16 17 18 19  20
 21 22 23 24 25 26 27 28 29  30
 31 32 33 34 35 36 37 38 39  40
 41 42 43 44 45 46 47 48 49  50
 51 52 53 54 55 56 57 58 59  60
 61 62 63 64 65 66 67 68 69  70
 71 72 73 74 75 76 77 78 79  80
 81 82 83 84 85 86 87 88 89  90
 91 92 93 94 95 96 97 98 99 100

Re: Why do arrays start at 0?

#284

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

It‘s an unsigned int, so past 0 it overflows back to the maximum

Re: Why do arrays start at 0?

#285
post #40

Dijkstra's answer (linked in the article) is best: "When dealing with a sequence of length N, the elements of which we wish to distinguish by subscript, the next vexing question is what subscript value to assign to its starting element. Adhering to convention a) yields, when starting with subscript 1, the subscript range 1 ≤ i https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...

It could just as easily be 1 ≤ i ≤ N and then I wouldn't have to remember that the lower bound is inclusive and the upper exclusive.

Then the delta of both bounds (N – 1) wouldn’t equal the length of the range (N). The inclusive-exclusive convention is used in order for `end = start + length` to hold.

Re: Why do arrays start at 0?

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

Re: Why do arrays start at 0?

#287
post #137

Earlier quoted context omitted.

the correct way/idiom to reverse iterate an array is for (size_t i = length; i-- > 0; )... It's surprising how often the issue pops, it works well with both signed and unsigned integers. (edit) I've started with one based indexing (basic)... mixed with 0 based (assembly), more 1 based (pascal), then more stuff (all zero based). I am, yet, to see a real advantage of a one based indexing... after the initial process.

Is this cache friendly?

It’s not. It was nice on architectures were cache didn’t matter much and were subtracting and comparing to zero was just one instruction (looking at you old core ARM)

Re: Why do arrays start at 0?

#288

Because otherwise you would be wasting a perfectly good number for no reason, which means you need to use more bits to do the same thing. To write 4 numbers (including zero) you only need two bits 0: 00 1: 01 2: 10 3: 11 To write 4 numbers if you avoid using the number zero, you need three bits 1: 001 2: 010 3: 011 4: 100 If you extrapolate that a little bit, you'll realize that you'll need two bytes (1 Byte + 1 bit…

[deleted]

Re: Why do arrays start at 0?

#289

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

It loops while the condition is true. When an underflow happens, it stops being true.

Re: Why do arrays start at 0?

#290

Earlier quoted context omitted.

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

It‘s an unsigned int, so past 0 it overflows back to the maximum

Ooh, i see. I wasn't aware that they under/overflow at 0 when they're unsigned. Thanks for broadening my horizon!
Post reply on HN