Live data from Hacker News

Why do arrays start at 0?

buttondown.email

361–370 of 702 posts

Re: Why do arrays start at 0?

#361
post #280

Earlier quoted context omitted.

https://linux.die.net/man/3/localtime : tm_mday: The day of the month, in the range 1 to 31. tm_mon: The number of months since January, in the range 0 to 11. I wonder where C got it? It goes back to at least 1973's V4: https://github.com/dspinellis/unix-history-repo/commit/92779...

https://minnie.tuhs.org/cgi-bin/utree.pl?file=V6/usr/source/... Convenience. Look at how the days in the months are stored and accessed. Using 1-based months would introduce an extra calculation (-1) on all searches or an unused value in the 0-index. Also look at how printing is handled for weekday and month names. They, again, take advantage of 0-based indexing. Day and year are already represented as numbers, so it…

Subtracting 1 from the user provided index isn't that expensive.

Or, if you're willing to give up three bytes, index into "ErrSunMonTueWedThuFriSat".

Or, and this is mildly insane but perhaps in keeping with early C, have your pointer be three bytes before the beginning of "SunMonTueWedThuFriSat" and use one-based indexing.

Re: Why do arrays start at 0?

#362

Earlier quoted context omitted.

That so people in this thread argue about the higher-level language (missing the point) shows that few people found access to the underlying machine code. Which is sad, because all code is still executed as machine instructions even when the developer does not see it or does not want to care.

We're 20 years past the point where you can expect everyone in tech to trace every instruction down to machine instructions. Higher level languages abstract away the need for it, and for the most part, we can rely on the authors of those languages to make many of the decisions that impact performance. The rest of us learn about these details on posts like this. It's not a sad fact, in fact it's probably one of the mo…

Not sure if you were around 20 years ago, but you sure couldn't expect everyone in tech to understand machine instructions as well as the layers above, all the way up to corporate politics.

But some people, yeah, they could do it.

That has not changed.

Re: Why do arrays start at 0?

#363
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.)

A more sensible English translation from Korean would be to use the phrase "in year X" rather than the phrase "X years old": a newborn is in year 1; after 12 months they are in year 2; etc.

In fact, this whole discussion is more about a choice of phrasing rather than the numbers. When indexing arrays, sometimes we're talking about an offset from the first element (starting with "0"), and sometimes we're talking about ordinal element numbers (starting with "first"). Some programming language designers found that offsets are more useful (because that choice tends to simplify the underlying arithmetic), while others found that ordinal numbers are more useful (because the word "first" should mean "1", to simplify communication between people).

Re: Why do arrays start at 0?

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

A big international corporation (Siemens) had, at one point, standardized their buildings world wide to have room numbers starting with 2 on the ground floor, 3 on the floor above etc., leaving space for two basements. All room numbers were 5 digits, and skipped numbers if there were multiple windows in one room, based on the theory that such a room might later be sub-divided, and wanting to avoid renumbering of room…

I worked for them relatively recently and didn't encounter this; or at least never noticed. Then again, our office building was an open office plan where desks next to the windows and rooms (conference or office) were all in the center of each floor.

Re: Why do arrays start at 0?

#365

Earlier quoted context omitted.

That so people in this thread argue about the higher-level language (missing the point) shows that few people found access to the underlying machine code. Which is sad, because all code is still executed as machine instructions even when the developer does not see it or does not want to care.

We're 20 years past the point where you can expect everyone in tech to trace every instruction down to machine instructions. Higher level languages abstract away the need for it, and for the most part, we can rely on the authors of those languages to make many of the decisions that impact performance. The rest of us learn about these details on posts like this. It's not a sad fact, in fact it's probably one of the mo…

[deleted]

Re: Why do arrays start at 0?

#366

Earlier quoted context omitted.

I asked my son to show me zero fingers(after show me 2 fingers and 5 fingers), he closed his fists.

My point exactly - there isn't an identifiable zeroth finger.

And? My solution? Let the kid grow up and learn.

Re: Why do arrays start at 0?

#369

Worst than 0 and 1 is to have the choice. In Visual Basic 6 you have the option between the base 0 or 1 for each module. It make debugging a nightmare and you can create bug just by copy/pasting code from one program to other with a different base index.

One of the least-loved APL features is the fact that you can change the index origin at runtime.

Re: Why do arrays start at 0?

#370

Earlier quoted context omitted.

That so people in this thread argue about the higher-level language (missing the point) shows that few people found access to the underlying machine code. Which is sad, because all code is still executed as machine instructions even when the developer does not see it or does not want to care.

“machine code” isn't really the zero point that's special nowadays. The code the programmer wrote is compiled to something such as LLVM IR, LLVM IR is further compiled by LLVM to Assembly, this is further compiled by an assembler into machine code, and then the c.p.u. further compiles this to it's internal code as it executes it. “machine code” really is no more special in this chain of events than, say, LLVM IR.

[deleted]
Post reply on HN