https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81801
--- Comment #3 from Petr Malat <oss at malat dot biz> ---
After reading the related part of the standard (actually, N1570 draft), I'm not
so sure about this. The semantics section in 6.5.6 says pointers have to be
from the same object and also the result must fit into the ptrdiff_t, which
range is [PTRDIFF_MIN, PTRDIFF_MAX], which has to be equal or larger than
[-65535, 65535].

So, theoretically it would be fine to use a shorter type for that, however
there is still a problem if the calculation is done as it is now for larger
objects as the difference is computed first at bytes and divided later by the
object size, so the overflow can still happen even the result fits in
[PTRDIFF_MIN, PTRDIFF_MAX].

One could solve it by dividing both pointers by the size before the
subtraction, but that would make the operation more expensive.

Reply via email to