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.
Why do arrays start at 0?
441–450 of 702 posts
Re: Why do arrays start at 0?
#442Earlier 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.
Re: Why do arrays start at 0?
#443It 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…
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?
#444Earlier 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.
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?
#445Earlier 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.
Re: Why do arrays start at 0?
#446Earlier 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?
Re: Why do arrays start at 0?
#447If 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.
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?
#448Earlier 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.
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?
#449Earlier 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.
Re: Why do arrays start at 0?
#450Earlier 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.
e.g. len(range(3)) == len([0, 1, 2, 3]) == 4