Is the behavior of the following code well defined?
int32_t i = -0x80000000;
uint32_t u = abs(i);
There may be an overflow/underflow on the first line. But on platforms when that is ok, does the second line have defined behavior?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Do you mean
-0x80000000with seven 0s? The behavior ofabs(-0x8000000)is certainly well-defined (bothuint32_tandint32_tcontain±0x8000000in their valid range if those types exist.When the standard says one behavior is undefined, it is undefined even if some particular platform work in some defined way.
The C++’s
absfunction is taken from C, and the C standard says (§7.20.6.1/2):So the result of
absis undefined. Pratically it will return0x80000000, but that’s not the behavior defined by the standard.