4 ms·
There's another reason that I haven't see anyone bring up. If your native unsigned integer type is the same size as a word (which is common), and a word defines
by eridius 13y ago
There's another reason that I haven't see anyone bring up. If your native unsigned integer type is the same size as a word (which is common), and a word defines the limits of your address space, and if you have a byte pointer initialized to 0, then 0-based indexing allows you to index into every byte in your address space, whereas 1-based indexing prevents you from indexing the final byte in your address space.
Additionally, assuming an unsigned integer type is used for indexing, 0-based makes every index valid and meaningful, whereas 1-based leaves one index value (0) as invalid.
- Dylan16807 13y agoHaving a null value that's invalid is a good thing. Thanks for the novel argument in favor of 1-based indexing!
- kenbot 13y agoErk, no it isn't. If your semantics require an optional value, then encode it that way explicitly. Don't inflict special cases and bugs on everyone else.
- deleted 13y ago[deleted]
- eridius 13y agoIt is? What possible reason is there to have an invalid value for an index parameter?
- Dylan16807 13y agoYou specifically said the native word size and address space. That's going to be a pointer, where a value that causes the CPU to trap is quite handy. And there's already plenty of invalid index values. -1 for example. There's no way to make an allocation the size of your entire ram, so 0xff...f is going to be invalid.
- eridius 13y ago> That's going to be a pointer, where a value that causes the CPU to trap is quite handy. You're confusing an invalid pointer with an invalid index. There is absolutely no reason for any index to ever be invalid. > And there's already plenty of invalid index values. -1 for example. -1 is not representable with unsigned integers. There are no invalid index values in a 0-based indexing system (using unsigned integers, as I initially stated). > There's no way to make an allocation the size of your entire ram, so 0xff...f is going to be invalid. This is so completely off the mark I'm surprised you even came up with it. There are several things wrong with this (off the top of my head): 1. I don't need an allocation the size of my address space in order to want to address both the first and last bytes in the address space. 2. You don't know what machine I'm running on. This could be an embedded 16-bit machine without multiprocessing with 64KB of RAM. Every single addressable byte is valid and corresponds to RAM storage. 3. It doesn't even matter whether or not a given address can be loaded. Indexing a pointer does not necessarily require loading it. As a trivial example, I could take the address of the resulting indexed value.
- Dylan16807 13y ago>You're confusing an invalid pointer with an invalid index. There is absolutely no reason for any index to ever be invalid. I'm not confusing them. You are very clearly describing a pointer. >-1 is not representable with unsigned integers. When I wrote -1 I meant "the value you get when you type -1 in your source code". >This is so completely off the mark I'm surprised you even came up with it. There are several things wrong with this (off the top of my head): >1. I don't need an allocation the size of my address space in order to want to address both the first and last bytes in the address space. That's a pointer issue. If I allocate 20 bytes near the end of ram, my last index is only going to be 19 or 20, so it doesn't matter if I can't go to UINT_MAX. > 2. You don't know what machine I'm running on. This could be an embedded 16-bit machine without multiprocessing with 64KB of RAM. Every single addressable byte is valid and corresponds to RAM storage. Again, that's a pointer. > 3. It doesn't even matter whether or not a given address can be loaded. Indexing a pointer does not necessarily require loading it. As a trivial example, I could take the address of the resulting indexed value. Taking the address is a pointer issue.
- lectrick 13y agoPlease cite an example where this is superior to a 0-based-index example. Just saying "it's a good thing" won't fucking fly around here.
- Dylan16807 13y agoI thought I explained myself, especially with my second comment, but sure I'll go over it again in detail. Premise 1: Accessing a null index is a logically invalid operation. Premise 2: Silent invalid operations are bad. Premise 3: If a null has a special representation that the hardware understands to be invalid, it can trap or set a flag or otherwise not be silent. Conclusion: Null indexes that are architecturally invalid to use are a good thing, because you can catch bugs involving their use immediately. Edit: made terms clearer
- eridius 13y agoPassing 0 as the index into an indexing operation is not even remotely "accessing a null value". Also, believe it or not, on various machines and OS's it's legal to map address 0. Edit: Your edit makes even less sense. There is no such thing as a "null index", much less an operation to "access a null index".
- Dylan16807 13y ago>There is no such thing as a "null index", Would you rather I write "index that is 0 in a world where indexes start at 1" over and over? I am willing to do that if it helps make the conversation smoother. >much less an operation to "access a null index". foo_array[0]; //operation to do an access with a null index >Also, believe it or not, on various machines and OS's it's legal to map address 0. Which is a bad thing. You get a couple extra variable slots in exchange for thousands of privilege-escalation bugs that could instead be crash bugs.
- lectrick 13y agoHow in the hell does "accidentally" accessing the first element of an array (by "accidentally" passing 0 to an array in a language that has 0-based indexing) result in "privilege escalation errors"? That's just "shitty programming". For that matter, how does a 1-based indexing language NOT result in far more "off by one" errors which would include the kind of errors commonly known as "buffer overflow" errors?