Assuming the pointer is equivalent to an unsigned integer, we see that the problem only exists if values started at address 0, in which case the pointer will UINT_MAX around after decreasing and become UINT_MAX .
To visualize the problem, let's go step by step, assuming values starts at 0x0:
iteration 1: vp = 0x4, *vp = 0; iteration 2: vp = 0x3, *vp = 0; iteration 3: vp = 0x2, *vp = 0; iteration 4: vp = 0x1, *vp = 0; iteration 5: vp = 0x0, *vp = 0; iteration 6: vp = 0xFFFFFFFF; *vp = ?? // uh oh!
Thus, vp will never be less than the minimum value for the pointer (which is 0), and this will lead to an infinite loop (assuming that all memory is writable) or a segmentation error.
This is also undefined behavior according to the standard (since you can address one element after the array, but not earlier than it), but in fact it should never fail in any realistic system.
Richard J. Ross III
source share