3 ms·
Use unsigned when indexing arrays, for two reasons: First, arr[(unsigned)-1] will segfault while arr[-1] will likely cause harder to debug non-local problems.
by dllthomas 13y ago
Use unsigned when indexing arrays, for two reasons:
First, arr[(unsigned)-1] will segfault while arr[-1] will likely cause harder to debug non-local problems.
Second, mod in C and C++ is broken(ish) on signed values: negative % positive returns a negative number. This means in order to use it as an index you need to check the value and add the table size post-mod if you want to wrap an index into an array (as you might for a hash table or ring buffer).
- duaneb 13y agoThis is ok in general, but IMHO silently failing code (like a break condition that never happens because of overflow) is far worse than getting your arithmetic right. Getting it wrong implies a clear bug, whereas a seemingly bugless loop might be harder to identify as the source of the error.
- dllthomas 13y agoI think you either misread me, or I'm misreading you. I'm advocating the method that fails immediately (unsigned, so an underflow hits unmapped memory immediately and segfaults).
- Someone 13y agoI don't see why arr[(unsigned)-1] is guaranteed to segfault. Care to explain? Isn't it equivalent to * (arr + (unsigned) -1) , which should be equivalent to * (arr + UINT_MAX) On typical PC hardware with sizeof(int) == sizeof(char *) that looks like a undefined behavior case to me (maybe barring border conditions; I don't feel like thinking of all edge cases). Most C compilers choose the fastest possible path for undefined behavior cases, so I would expect this to (typically. Again, there will be border cases) be equivalent to arr[-1]. (See also http://stackoverflow.com/questions/2578455/pointer-arithmetic-signed-unsigned-conversions http://stackoverflow.com/questions/2578455/pointer-arithmeti...)
- dllthomas 13y ago"Typical PC hardware", at least in my recent experience, has sizeof(int) == 4, sizeof(char*) == 8 and so the reason they're different should be apparent. I believe you are correct that there is no difference when they are equal.