In my opinion, this is from electronics, isn't it?
Why did it all go?
- eightSuppose you have an array of integers, each of four bytes. The array is located in memory sequentially. To get the element N, you need to take a pointer to an array and add to it (N - 1) * 4 bytes, or if you count from zero, just N * 4 bytes - etki
- one@Etki: Why not as an answer? - VladD
- In electronics, the counting of the legs of many microcircuits also starts from 0. I thought it was connected ... - faoxis
- oneOn the ruler, the first digit is also 0! the coordinate of the first value starts at zero. - Andrey Shpilevoy
- oneI'm too lazy to translate, but most of the serious reasons are given here quora.com/ ... And there is a link to Dijkstra's article - strangeqargo
1 answer
First of all, because from the point of view of implementation, the concept of an “index” of an element in a continuous aggregate is directly related to the concept of “displacement” of an element from the beginning of the aggregate in memory. It is clear that the offset of the very first element of the unit is zero. Accordingly, its index is reasonable to take equal to zero.
In other words, no matter how twisted, but the index of the first element in the aggregate will still have to be recalculated to the value 0
at the machine level. In such a situation, the implementation of indexation with a non-zero base will always, willy-nilly, imply bringing it to indexation with a zero base. In many programming languages, they simply do not consider it necessary to do this. Why really?
- When allocating dynamic memory in C / C ++, the returned pointer does not necessarily indicate the beginning of the allocated section of memory. - hunter
- @hunter: What does it matter in this case? The main thing is that the pointer points to the beginning of the user memory area, i.e. to the point from which the offset will be measured. And the fact that the unit can also contain some service areas with service data does not refer to this issue in any way. What exactly did you want to say? - AnT
- then I want to say that even when allocating an un-fragmented memory area from a pointer to the zero element, sooner or later it may be necessary to obtain a pointer to a memory cell with a smaller address. - hunter
- @hunter: Great. But what does this have to do with indexing? The “cell with a smaller address” is not part of the user data, and no one will access it (by using a negative indexation of the user’s mind). Such a memory area will be perfectly accessible by subtracting some known platform offset from the user level pointer and that's it. I still don’t see how it all has to do with the topic or my answer. - AnT
- 3@avp: Before C, there were B and BCPL languages with the same zero indexing. The C language inherited its approach to arrays from B and BCPL, with one important change. In B and BCPL, arrays were physically implemented via regular pointers. In C, the physical pointers were removed, but they became “emulated” (the notorious “array type decay”). And everything else in C remains, as in B and BCPL, precisely to facilitate backward compatibility with these languages. - AnT