[Submission] D Slices

Timon Gehr timon.gehr at gmx.ch
Tue May 31 13:23:54 PDT 2011


eles wrote:
> > > Actually, I think 0-based vs. 1-based is of little importance in
> the
> > > fact if limits should be closed or open. Why do you think the
> > > contrary?
> > Good question. Actually, I'm not too sure if 1-based is better
> suited
> > with closed. But I can say that it makes a lot of sense for 0-based
> > arrays to be open.
> > Take this array of bytes for instance, where the 'end' pointer is
> > located just after the last element:
>
> However, if the length of the array is exactly the maximum value that
> can be represented on size_t (which is, I assume, the type used
> internally to represent the indexes), addressing the pointer *just
> after the last element* is impossible (through normal index
> operations).

I already disproved you on this, why do you keep insisting it is a valid point?
Did you miss my post?
In more detail:
Assume your size_t has 3 bits. Ergo, you can index into 8 memory locations, namely
0,1,2,3,4,5,6,7.
The maximum value that can be represented in 3 bits is 7.

You have a byte array of that maximum length that takes up memory locations
0,1,2,3,4,5,6. You can point to the end of your array in that case, which is
location 7.
Yes, of course, your array could end just at location 7, but that is a different
issue (and a trivial one, just don't allocate an array that ends at location 7).
What is the point? (You can slice quite peacefully without having to care about
the element that is off by one anyways. You don't need that pointer for slicing.)

>
> Then, why characterizing an array should depend of something... "just
> after the last element"? Whatif there is nothing there? What if, on a
> microcontroller, the last element of the array is simply the highest
> addressable memory location? (like the "end of the world") Why
> introducing something completely inconsistent with the array (namely,
> someting "just after the last element") to characterize something
> that should be regarded as... self-contained? What if such a concept
> (data after the last element of the array) is simply nonsense on some
> architecture?

Give up the last word of memory for arrays if you need that pointer. Store
something different there. It has already been done for the first word on most
platforms I am aware of. If you just personally like the other way of slicing
more, why don't you name it by that?
The latest arguments you have given are esoteric enough that I am starting to
suspect it is the case. Tell me if I'm wrong.

Are there any _practical_ drawbacks of half-open intervals? There are many
_practical_ benefits.

Timon


More information about the Digitalmars-d mailing list