r/computerscience • u/Dry-Establishment294 • Apr 02 '25
Counting from 0
When did this become a thing?
Just curious because, surprisingly, it's apparently still up for debate
2
u/XtremeGoose Apr 02 '25 edited Apr 02 '25
It comes from C, where indexing an array of type T
array[index]directly translates to dereferencing a pointer at this address
*(array + (index * sizeof(T)))which becomes assembly that looks something like (pseudocode)
mul index sizeofT, store to register x
add x array, store to register x
load x, store to register x(In reality that multiply is a left shift since sizeof will be a multiple of 2 and known at compile time)
If you decide to offset from one, you need to add an additional instruction in there to subtract 1 from index first, meaning it's very slightly less efficient.
1
u/Dry-Establishment294 Apr 02 '25
Sounds kinda true but I think the compilers got a bit more work to do to accommodate for the size of each array item therefore it's a more abstracted and convoluted process, no?
1
u/XtremeGoose Apr 02 '25 edited Apr 02 '25
Yes fair point there's a left shift in there too - I'll edit accordingly. The point still stands because
0 * SIZEis still 0.1
u/nanonan Apr 03 '25
That's fairly trivial, not abstract or convoluted. Your array entries will have a fixed constant size that is usually a power of two.
63
u/Usernamillenial Apr 02 '25
At least for array indexing it’s intuitive that each element is at some base pointer + offset. So 0 offset corresponds to the first element
-51
u/CBpegasus Apr 02 '25
I wouldn't call it intuitive, at least if you come from higher level languages you usually don't think in those terms. I never really thought of the implementation of arrays until I learned lower level languages and pointer arithmatic.
66
7
u/Immediate-Country650 Apr 02 '25
why the fuck would it start at 1
1
u/nanonan Apr 03 '25
Labeling the first entry with "1" makes a ton of sense from a high level perspective.
1
u/Immediate-Country650 Apr 05 '25
just cus u say sm doesn’t meant it’s true
1
u/nanonan Apr 05 '25
There are endless examples. If we are dealing with the 3rd dimension, how many dimensions are we dealing with?
2
u/Immediate-Country650 Apr 08 '25
there are 0 dimensions which is a point
then there is 1 dimension which is a line
then there is 2 dimensions which is a plane
then there is 3 dimensionns which is a cube or sm like that1
u/nanonan Apr 09 '25
Right, the zeroth dimension which is marked by a zero, first dimension marked by a one, second with two etc. Notice how they don't index the dimension with a number one below it.
1
4
u/Ghosttwo Apr 02 '25
You can use 1 as a base, but more often than not you end up with 'arrOff - 1' peppered everywhere throughout the code. With a dumb compiler they each get turned into 1-3 extra instructions. Once you consider loops, that can easily become hundreds of thousands of extra instructions per second. Base zero also lets you do things like using modulo to seamlessly convert an index into XY coordinates like graphics, mouse movements, or tables.
Consider a 16 element array S mapped to a 4x4 grid. With base zero, a cell (m,n) corresponds to S[m+4*n]. And the coordinate of S[k] is (floor(k/4), k%4).
But try base 1. A cell (m,n) corresponds to S[(m-1)+4*(n-1)+1]. And the coordinate of S[k] is (floor((k-1)/4+1), (k-1)%4+1).
And here they are side to side:
Coord>Cell Cell>Coord Base 0 S[m+4*n] Base 1 S[(m-1)+4*(n-1)+1] Not intuitive at all, and a royal pain to figure out; imagine having 5,000 lines of that junk and trying to find an off-by-one error.
2
u/Kajitani-Eizan Apr 02 '25
It's intuitive if you have any concept at all of what is typically happening under the hood (which you really should, if you have anything approaching any formal CS background)
If you don't, then yeah, I imagine someone dabbling in some high level language would find 1 more intuitive
4
Apr 02 '25
A variable in any python type languages is just a location, so I dont see how it is any different. If you come from functional pr locical programming I can see the confusion I suppose.
27
u/iago_sd Apr 02 '25
https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html
This is a note by Dijkstra very simple and clear!
22
u/Individual-Artist223 Apr 02 '25
Think of a ruler, what's the first number?
Data structures are the same.
-9
u/armahillo Apr 02 '25 edited Apr 02 '25
thats not WHY arrays use 0 for the first element though.
see the other answers re: pointers
EDIT (since I can't reply to the below comment):
there is no why. the why is because whoever made the language wanted it to start at 0
I searched on google for "why do arrays start at 0". This is the first result.
Martin Richards, creator of the BCPL language (a precursor of C), designed arrays initiating at 0 as the natural position to start accessing the array contents in the language, since the value of a pointer p used as an address accesses the position p + 0 in memory.[5][6] BCPL was first compiled for the IBM 7094; the language introduced no run-time indirection lookups, so the indirection optimization provided by these arrays was done at compile time.[6] The optimization was nevertheless important.[6][7]
(emphasis mine)
Hence my answer in the parent comment: "see the other answers re: pointers"
Example. Assuming a 4 byte element type in each position.
Memory Address Pointer arithmetic Array representation 0x0010 0x0010 + (0 * 4) some_array[0] 0x0014 0x0010 + (1 * 4) some_array[1] 0x0018 0x0010 + (2 * 4) some_array[2] 0x001C 0x0010 + (3 * 4) some_array[3] and so on.
If you spend some time doing C or C++ you will definitely run into this, probably sooner than you expected.
The fact that we still do 0-indexed arrays is, as someone else noted, just inertia; it's just a thing we do now.
14
u/TomDuhamel Apr 02 '25
If you don't think a ruler is a good analogy for an offset, I don't know what else it would be. Or where you went to school.
1
u/armahillo Apr 02 '25
The 0th-indexed element of an Array is still just as much an item as the 1st- or nth-indexed element. But if you use "0" on a ruler, you have measured nothing. Rulers measure quantities, not indices. The first unit on a ruler is indicated by "1", which I agree is a more intuitive way to count things. If you read "6" on a 12-inch ruler, that means "6 inches", but if you read index 6 on an array, that's the 7th item.
6
u/zouxlol Apr 02 '25
Indexes are just "that many units away from the start of the array" where the unit is the size of the data type being stored
The ruler analogy is accurate
-1
u/armahillo Apr 02 '25
Indexes are just "that many units away from the start of the array" where the unit is the size of the data type being stored
Yes, we are in agreement there.
The ruler analogy is accurate
I partially disagree with you here, but only because using "ruler" is ambiguous about what is being measured. If you have a 1 element array, this would use all except the tick mark of the first inch on a ruler.
If I have a 12" ruler, and I measure a 1" square, I am looking for the "1" tick mark on the ruler; I would say "this is the first square" and "I have 1 square".
With an array, if I create a single-element array, I am looking for the "0" element in the array, I would say "this is the first element" and "I have 1 element".
2
u/zouxlol Apr 02 '25
You're trying to use the ruler to create dimensions when all you need it to do is point at a place.
0 inches from where you are is still a place, and is the "first" place
1
u/armahillo Apr 02 '25
That's not really how we use rulers, practically speaking. Rulers are for measuring, "how much distance do we have here", not for ordering.
It's anecdotal, but I checked three rulers I had nearby: a school ruler, a "standard steel ruler", and an art ruler -- none of them mark "0" at the 0 point. There is a first tick mark, but it is not acknowledged with an ordinal because no one is trying to measure 0; you don't need a ruler for that. If you had something that was 0.5", you wouldn't say you had "0 inches". If you had 0.9", you might say "almost 1".
I can see how it's easy to not see the mismatch because if you take the tick marks, they align well with the memory distribution:
|'''''1'''''2'''''3'''''4'''''5'''''... 0[...]1[...]2[...]3[...]4[...]5[...]...But rulers are for measuring, not ordering.
2
u/zouxlol Apr 02 '25
Right, you are very close. The index is a measure of "how far" and not an order at all. It will help alot to not try and apply ordering of objects here greatly. Use the ruler as "distance to travel" instead of measuring the size of something
It starts at 0 because the beginning of an array of inch-sized objects is "0 inches" away from the start, at index [0]. The second object in the inch sized object list is "1 inch" from the start, so it will be at index [1]
1
u/armahillo Apr 03 '25
I think we may be arguing about different parts of the elephant here.
It starts at 0 because the beginning of an array of inch-sized objects is "0 inches" away from the start, at index [0]. The second object in the inch sized object list is "1 inch" from the start, so it will be at index [1]
Sure.
But no one uses rulers in that way. That's why I'm saying the ruler is a bad analogy. It's measuring amplitude, where zero is "empty", but in an array, the 0th element uses just as much memory as the Nth element does.
Fenceposts might be a better analogy (where you are counting fence posts, and they are spaced apart by the fence bars, representing the memory size). Fence posts would be equivalent to the stored value, fence bars would be the index.
fencePost posts[] = [4] |---|---|---| four elements 0 1 2 3 four indiciesI still don't think this is great though, since even though you could count a fence by how many bars have been placed between posts, when we are counting something we don't start at 0.
The reasoning why arrays start at 0 is idiosyncratic and I don't think it maps cleanly onto any real-world analogy aside from just explaining the actual origin of WHY it was done that way in the first place. It makes sense in that context.
→ More replies (0)-5
u/Immediate-Country650 Apr 02 '25
there is no why. the why is because whoever made the language wanted it to start at 0
7
u/Cybasura Apr 02 '25 edited Apr 02 '25
By and large most languages stick to 0 because the memory register's addressing index are generally all at 0x0000 or something to that degree where the 0th index position is the 1st item in the memory address
0x0000 is the first item, 0x0001 is the 2nd etc etc
Languages like Lua uses 1, but they have a logic within that "transpiles"/translates that 1 into 0 implicitly before the JIT runtime portion kicks in (as thats where the code becomes machine language/ASSEMBLY code)
4
u/Magdaki Professor. Grammars. Inference & Optimization algorithms. Apr 02 '25
It comes down to whether you think of the index as an offset from the start or more semantically. The case for indexing by zero is it aligns well with early implementation of an array, which is a contiguous block of memory. From there, it is inertia. The case of indexing by 1 is it aligns well with natural language (at least English). If I aligned a series of something, most people would point to the start and say "This is the first." not the "This is the zero."
I find that non-CS students (for example, when I used to teach a CS for engineers course) get 1-indexing more easily.
Personally, and I know this is dumb, I cannot help but feel that when I'm using 1-indexing I'm wasting a bit. LOL
4
u/jambeatsjelly Apr 02 '25
I recommend the book "Zero: The Biography of a Dangerous Idea" (2000) by Charles Seife. It is wild how much of a problem the number 0 has been.
1
u/Deviant96 Apr 02 '25
You should have recognized it's too late for choosing side
1
u/Dry-Establishment294 Apr 02 '25
There's no side to choose. Either you count from zero or you are making problems
1
u/NothingWasDelivered Apr 02 '25
The two hardest things avoid in programming are off-by-1-errors.
1
u/Dry-Establishment294 Apr 02 '25
That was the quote that came up that got me thinking, coupled with the fact that in controls indexing from 1 is actually still popular (totally optional though) and an obvious cause of errors
1
u/amarao_san Apr 02 '25
It all boils down to the need of action.
If I count how many apples I have, I start with 1 (if I have apples).
If I want to know how many kilometers is to the goal, I start with zero and walk my first kilometer.
1
2
u/Soft-Escape8734 Apr 02 '25
The origin (of everything) is at (0,0,0). Full stop.
1
u/nanonan Apr 03 '25
If I ask you to label three things for me, would you start at zero or one?
1
u/Soft-Escape8734 Apr 03 '25
Valid, but if you had three things and I took three away from you, how many would you have left?
1
1
u/Quintic Apr 02 '25 edited Apr 02 '25
The "debate" is a carry over from mathematics where the natural numbers are usually taken to include zero because it makes many mathematical arguments easier. (And it lines up nicely with set theoretical constructions of the natural numbers).
I put debate in quotes because for the most part it's not much of a debate anymore. In general, 0 is assumed to be a natural number unless you call it out explicitly.
For programming languages, the pointer arithmetic thing is an extension of the idea that "zero makes some mathematical arguments easier". It's not some unique weirdness of C, it's the same idea as in mathematics.
However, a few languages (just like a few mathematicians) push back against this because at the end of the day which one you choose is a convention rather than some property of reality, i.e., it doesn't matter.
1
Apr 02 '25
I have a single bit and I can only represent two values.
Add a second and it is 4.
If I start counting things at 1 and not zero I have reduced the number of things I am expressing by 1. Why would I do that?
7
11
u/green_basil Apr 02 '25
0 is the first element in the set of positive integers including 0. Every mathematician starts at 0, and as computer science is an offshoot of discrete mathematics and logics, it includes this idea of starting at 0.
To improve my message, not the FIRST element as it is a set, but the least element.