Given the following in C++(you might be surprised):
unsigned short x=0xFFFF;
unsigned short y=0xFFFF;
auto z=x*y;
std::cout << z;
Without checking, assuming an LP64 system what is the result:
A. 4294836225
B. -131071
C. fffe0001
D. Undefined behavior
#Cplusplus
#Cpppolls
-
Prikaži ovu nit
-
First of all big h/t to
@Myriachan whom I learned this devious example from
The answer is D, this is undefined behavior but how you might protest, unsigned number don’t have undefined behavior on overflow, do they?6 replies 3 proslijeđena tweeta 19 korisnika označava da im se sviđaPrikaži ovu nit -
This is an unfortunate result of the Usual Arithmetic Conversions: https://en.cppreference.com/w/c/language/conversion#Usual_arithmetic_conversions … Before performing binary operations we need to bring the operands to a common type via the Usual Arithmetic Conversions. See [expr.mul]p2 http://eel.is/c++draft/expr.mul#2 …pic.twitter.com/oWGx6V6fol
1 reply 1 proslijeđeni tweet 8 korisnika označava da im se sviđaPrikaži ovu nit -
and [expr.arith.conv] http://eel.is/c++draft/expr.arith.conv … Then we hit the integral promotions [expr.arith.conv]p1.5 http://eel.is/c++draft/expr.arith.conv#1.5 … and then [conv.prom]p1 http://eel.is/c++draft/conv.prom#1 …pic.twitter.com/wPrKNB5gyE
1 reply 0 proslijeđenih tweetova 3 korisnika označavaju da im se sviđaPrikaži ovu nit -
We also need [conv.rank]p1.3 http://eel.is/c++draft/conv.rank#1.3 … to tell us that the rank of short int is < int Which finally tells us that both operands will be converted to int and we end up with signed integer overflow which is undefined behavior, see [expr.pre]p4 http://eel.is/c++draft/expr#pre-4 …pic.twitter.com/DoIPqHEwP5
1 reply 0 proslijeđenih tweetova 6 korisnika označava da im se sviđaPrikaži ovu nit -
Alternatively casting one of the operands to a larger unsigned type would have results in both operands becoming unsigned e.g. auto z = static_cast<unsigned>(x)*y; Also see the following godbolt: https://godbolt.org/z/LyfLi- pic.twitter.com/P352YITBQ2
1 reply 0 proslijeđenih tweetova 6 korisnika označava da im se sviđaPrikaži ovu nit -
We could have caught this using a couple of methods. First if we were using UBSan this would have generated a runtime failure:https://godbolt.org/z/d2CBrk
1 reply 0 proslijeđenih tweetova 2 korisnika označavaju da im se sviđaPrikaži ovu nit -
Shafik Yaghmour je proslijedio/a tweet korisnika/ceShafik Yaghmour
If we used constexpr in a context that required a constant expression this would have generated a compile-time diagnostic: https://godbolt.org/z/WCi4BA Also see “Exploring undefined behavior via constexpr”:https://twitter.com/shafikyaghmour/status/1127219879457636353 …
Shafik Yaghmour je dodan/na,
0 proslijeđenih tweetova 3 korisnika označavaju da im se sviđaPrikaži ovu nit
Also see “Why must a short be converted to an int before arithmetic operations in C and C++” https://stackoverflow.com/a/24372323/1708801 …pic.twitter.com/hfcr9IBcN1
-
-
Odgovor korisniku/ci @shafikyaghmour
I do not agree with that. In the ISO standard, the behavior is define clearly. https://isocpp.org/wiki/faq/cpp11-language#auto … You have been mixing the old meaning of auto with the new one. It is clear on this page that you need to use only the new meaning.
1 reply 0 proslijeđenih tweetova 1 korisnik označava da mu se sviđa -
Odgovor korisniku/ci @FPiednoel
Shafik Yaghmour je proslijedio/a tweet korisnika/ceShafik Yaghmour
Yes, I should have specified C++11 or greater:https://twitter.com/shafikyaghmour/status/1222171449521139712 …
Shafik Yaghmour je dodan/na,
0 replies 0 proslijeđenih tweetova 0 korisnika označava da im se sviđa
Kraj razgovora
Novi razgovor -
Čini se da učitavanje traje već neko vrijeme.
Twitter je možda preopterećen ili ima kratkotrajnih poteškoća u radu. Pokušajte ponovno ili potražite dodatne informacije u odjeljku Status Twittera.

