4 ms·
I 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
by Someone 13y ago
I 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.