Which type in C should be used to represent the difference between two objects\' sizes?
As size_t
is unsigned, something like
size_t dif
When I am really worried about overflow issues like that (especially when working in modular arithmetic where "negative" values wrap somewhere other than ~0
) I just split it into two cases:
if (a > b) {
size_t diff = a - b;
} else {
size_t diff = b - a;
// code here subtracts diff rather than adds it, etc.
}