Ugh. Please no. This only adds to the confusion, as now there are 2 ways in which indices can be interpreted.
Specifically, this: >"Indexing between elements, instead of indexing elements, helps avoid a large class of off-by-one errors." It only replaces them with indexing-method errors. Instead of remembering if my ranges are open or closed, I have to remember if they are using between-element indices or on-element indices. It's still going to cause the same kinds of problems.
Indices Point Between Elements
31–40 of 66 posts
Re: Indices Point Between Elements
#32Earlier quoted context omitted.
_It used to be popular, and still is in some circles, to debate whether programming languages ought start array indexing at 0 or 1_ this is an exemplary case of citation needed if I ever saw one. maybe it's a valid debate for programming languages that doesn't allow people to do pointer arithmetic, which already restrict the field a lot, but even then that's sound as part of the 4GL bullshit that never really took of…
> sound as part of the 4GL bullshit Actually it's primarily early languages plus Lua. [0] https://en.m.wikipedia.org/wiki/Comparison_of_programming_la...
Re: Indices Point Between Elements
#33The visuals are definitely valuable in explaining this. It used to be popular, and still is in some circles, to debate whether programming languages ought start array indexing at 0 or 1. When talking about this with other programmers, I've discovered that a lot of the issues/confusion could be avoided by consistent use of terminology: Offsets/offsetting always being zero-based and indexes/indexing always being one-ba…
_It used to be popular, and still is in some circles, to debate whether programming languages ought start array indexing at 0 or 1_ this is an exemplary case of citation needed if I ever saw one. maybe it's a valid debate for programming languages that doesn't allow people to do pointer arithmetic, which already restrict the field a lot, but even then that's sound as part of the 4GL bullshit that never really took of…
Visual Basic had the "OPTION BASE" statement to select.[0] (Many other versions of basic did too)
APL also has the ⎕IO Index Origin setting [1]
If you want to see a lively debate, there's c2[2], and there's also Dijkstra[3]
[0] https://msdn.microsoft.com/en-us/library/aa266179%28v=vs.60%...
[1] https://en.wikipedia.org/wiki/APL_syntax_and_symbols
Re: Indices Point Between Elements
#34Earlier quoted context omitted.
also this works only in this special case of array being one sized. an index is an offset to a pointer in memory, shifted by the size of the structure it points to. there is no other way around, no magic tricks about index being between elements. of course people never exposed from c miss out all of this, and then are left to made up bullshit about how stuff actually works array index are offset to a memory location,…
Memory addresses are exactly analogous to array indices, and suffer from exactly the same semantics issues. After all, low-level memory is just an array of bytes. We do have a convention that dereferencing a memory address returns the 8 bits to the right of that address. We've even optimized our hardware for that convention. But that's just a convention of the dereference operation; it's not fundamental to the addres…
for example, if you have the following structs
typedef struct { void *key; } base;
typedef struct { base b; int misc; int data[2]; } derived;
then derived is laid out as follows -----+------+---------+---------+---------+-----
... | base | derived | data[0] | data[1] | ...
-----+------+---------+---------+---------+-----
^ ^ ^
| | |
base derived.data &derived.data[2]Re: Indices Point Between Elements
#35Earlier quoted context omitted.
Memory addresses are exactly analogous to array indices, and suffer from exactly the same semantics issues. After all, low-level memory is just an array of bytes. We do have a convention that dereferencing a memory address returns the 8 bits to the right of that address. We've even optimized our hardware for that convention. But that's just a convention of the dereference operation; it's not fundamental to the addres…
A pointer points to the start of its pointee - i.e. the point "just before" its pointee. That's how derived-to-base casts work. That's also how you can have "one-past-the-end" pointers, which are actually "just after" the relevant array. for example, if you have the following structs typedef struct { void *key; } base; typedef struct { base b; int misc; int data[2]; } derived; then derived is laid out as follows ----…
Re: Indices Point Between Elements
#36Earlier quoted context omitted.
A pointer points to the start of its pointee - i.e. the point "just before" its pointee. That's how derived-to-base casts work. That's also how you can have "one-past-the-end" pointers, which are actually "just after" the relevant array. for example, if you have the following structs typedef struct { void *key; } base; typedef struct { base b; int misc; int data[2]; } derived; then derived is laid out as follows ----…
Yep. A pointer is a memory range, but it supports a cast operation that allows you to change the pointer type, and therefore the end address. I think we agree, right? :)
Re: Indices Point Between Elements
#37The visuals are definitely valuable in explaining this. It used to be popular, and still is in some circles, to debate whether programming languages ought start array indexing at 0 or 1. When talking about this with other programmers, I've discovered that a lot of the issues/confusion could be avoided by consistent use of terminology: Offsets/offsetting always being zero-based and indexes/indexing always being one-ba…
Of course, it's the offset from the first element, so it's kind of a circular definition.
Re: Indices Point Between Elements
#38The visuals are definitely valuable in explaining this. It used to be popular, and still is in some circles, to debate whether programming languages ought start array indexing at 0 or 1. When talking about this with other programmers, I've discovered that a lot of the issues/confusion could be avoided by consistent use of terminology: Offsets/offsetting always being zero-based and indexes/indexing always being one-ba…
I remember something about the ground floor in the UK buildings not being "Floor 1" like it is in the United States. Actually, that's perfectly explained with your offset vs. index terminology. In some countries, the floor number is an index within the array of floors. In others, it's an offset from the ground.