Link to home
Start Free TrialLog in
Avatar of ckurt99
ckurt99

asked on

Testing for USHORT variable overflow

As part of a bit of defensive programming, I would like to use an assert to make sure that when I cast a ULONG into a USHORT variable that I won't be overflowing the size of numbers that a USHORT can handle.  Will the following work, do you think?  I think it needs the cast to ULONG so that the comparison is done as a ULONG.

#define MAX_USHORT    ((ULONG) ~((USHORT) 0x0))

ASSERT(myushortvar <= MAX_USHORT);
ASKER CERTIFIED SOLUTION
Avatar of BlackDiamond
BlackDiamond

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
If myushortvar is USHORT the ASSERT will always be true
Avatar of ckurt99
ckurt99

ASKER

Thank you for pointing that out, ozo. I should have used result_of_ulong_calc instead of myushortvar.