OP: Why support ... a potential memory violation?
It has potential uses, for as OP says it is a potential violation and not certain memory violation. C is about allowing users to do many things, include all the rope they need to hang themselves.
OP: ... throw a Negative Index warning ...
If concerned, use unsigned index or better yet, use size_t.
OP ... calculation done in runtime?
Yes, quite often as in a[i], where i is not a constant.
OP: hint at its uses?
Example: one is processing a point in an array of points (Pt) and want to determine if the mid-point is a candidate for removal as it is co-incident. Assume the calling function has already determined that the Mid is neither the first nor last point.
static int IsCoincident(Pt *Mid) {
Pt *Left = &Mid[-1]; // fixed negative index
Pt *Right = &Mid[+1];
return foo(Left, Mid, Right);
}
SIZE_MAXto a pointer (which has UB) has the same effect as adding-1to that pointer.(unsigned) (~0)will immediately cause an out-of-bounds violation.unsignedandsize_tare the same size,x[-1]andx[(unsigned) ~0]typically have the same effect in common implementations. Ifsize_tis wider thanunsigned(which is not uncommon in “64-bit” implementations) there is typically a difference; the former is usually just before the array, and the latter is a great distance away. However, one should generally avoidintorunsignedfor indices and preferptrdiff_t. When usingptrdiff_t, there is typically no difference.