I arrived at the point where I need to compare the values with and without a sign. So far, I have always changed the code base to completely avoid this situation, but now I can not do it.
So what is the really correct way to handle a folded and unsigned comparison? This is a mixed C / C ++ code base, so my question applies to both languages.
I check the resource (signed) for the requested value (unsigned).
if (requested > resource.max) return Never; if (requested > resource.free - resource.assigned) return NotNow; return Now;
I was thinking of something similar (replace C ++ options where applicable):
if (requested > (unsigned)INT_MAX) bail_out(); // assert,abort,throw,return.... if ((signed)requested > resource.max) return Never; if ((signed)requested > resource.free - resource.assigned) return NotNow; return Now;
Am I approaching this correctly, or is there a better way?
c ++ c casting unsigned signed
Let_Me_Be
source share