Will this expression correctly handle overflow?
size_t mid = (low + high) / 2;
size_t mid = (low + high) / 2;
if (size + size < size) {
return NULL; // size too big; avoid overflow
}Since this is integer division, I think you can do:
size_t low_s = low >> 1;
size_t high_s = high >> 1;
size_t mid = (low_s + high_s)
...With no overflow worries. (expanded for clarity)That would effectively be distributing the division. So it's equivalent to low / 2 + high / 2.