When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
106 messages ยท Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
@torn condor
Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.
#include <iostream>
using namespace std;
int main ()
{
int x, y;
x = 5;
y = ++x * ++x;
cout << x << y;
x = 5;
y = x++ * ++x;
cout << x << y;
return 0;
}
this code has undefined behavior.
wdym?
.
that the result of it is undefined
y = x++ * ++x;
stuff like this is also just not great
no i added it in a c++ ide it gives answer 749735
thats not what undefined means
yh but take for example this line
int x, y;
x = 5;
y = ++x * ++x;
what should the value of y be?
I believe that one is mostly fine, it's the one later that's problematic
yh but what could y be?
afaik this one is just 6 * 7 or 7 *6
yh but it gives 49 as answer
;compile
#include <iostream>
using namespace std;
int main ()
{
int x, y;
x = 5;
y = ++x * ++x;
cout << "X:" << x << " "<< "Y:" << y << "\n";
x = 5;
y = x++ * ++x;
cout << "X:" << x << " "<< "Y:" << y << "\n";
return 0;
}
X:7 Y:49
X:7 Y:35
you see
the result isn't 749735.....
no but for the first part x becomes 7 and y becomes 49 ryt?
you said 42 i thought 42 too
๐ค right no it would make sense if the final result was 7 for both of them
anyhow doing shit like this on 1 line is asking for trouble cause of exactly this reason
actually thinking about it it makes a lot of sense ๐ค and my assumption was propably wrong
The simplest solution is simply don't write confusing code like this and it's easy to reason about
that's the point, there's no value that y should be here.
the behavior of this code is undefined.
wtf i didn't write this
its a question i got as homework as an mcq
Then just awnsers the end result is UB :P
what kind of reply is this
I didn't mean with that you wrote it
so you dont know?
bruv
But all I was saying with that is by simply not writing confusing code like this you solve the problem of ambiguity
i'm saying why did y have a value of 49 instead of 42
Where is this hostility coming from
i'm asking you a question you're not answering properly
It's as dot was saying, there is no guaranteed value Y is supposed to be
again, this code has undefined behavior
Because it propably evaluated x to be 7
We are
what value should y be
it's undefined
You do not know, it's undefined
how so????
because the rules of C++ say so
It's undefined innit cause you can't know what the value of X it will use to evaluate it right? So 7x7, 6x7 and 7x6 be equally valid no?
i have an mcq question telling me this lines of codes and has choices
you want me to say undefined??
Then the question is wrong most likely
there's no correct answer
wait but hasn't x been assigned a value of 5
y should computate using that value ryt?
no...
why
basically, it's undefined in which order operands are evaluated. in your example, it's undefined at which points and in which order during evaluation of the expression the side effects of modifying the value of x take place.
yh but when compiled it gave an answer
Yeah, thats what undefined does
Say you have a million options when you compile it gives you one of them
Undefined behavior occurs when you violate rules specified by the language. For example: Reading uninitialized memory, performing out-of-bounds memory access, signed integer overflow, and race conditions.
int i; // default init,
// indeterminate value
while(i < 10) {
printf("%d\n", i++);
}
int arr[10];
for(int i = 0; i < 20; i++) {
arr[i] = i; // [i] out of
// bounds
}
Compilers are not required to provide warnings or errors about UB. Often it is undetectable at compile-time. Performing actions which are UB can render your entire program's behavior undefined, leading to anything from crashing to summoning Eldritch Abominations.
Sanitizers can help identify UB. For more info, look into address sanitizer (ASan) and undefined behavior sanitizer (UBSan). See !howto asan
It seems like the compiler ended up making it 7*7
++x increments x.
if you wrote x = 5; y = ++x; then both x and y would be 6
but
++x * ++x increments x twice without a sequence point
so it's undefined
ah ok
Or it can print "Lol, fix your program bro"
ok i'm getting it now
it's ub, so it doesn't have to be anything.
the compiler could also just optimize away your whole code
Fair, though I'd not expect it to go to far of the rails here
๐ really sorry for being aggressive and insulting you
it could also be 6x6 because the two reads and writes are unsequenced
i'm really sorry i'm new at c++ and really confused
Yeah all good
But yeah the entire reason I said don't so this, is cause stuff like this becomes a minefield
ok
Blegh I swear c++ sometimes
it could also be 42ร-1
most-likely, it would just not be anything the moment you turn optimizations on
Yeah, fair
;asm -O
int a (int x) {
return ++x * ++x;
}
int b (int x) {
auto inx = [&] { return ++x; };
return inx() * inx();
}
a(int):
lea eax, [rdi+2]
imul eax, eax
ret
b(int):
lea eax, [rdi+2]
add edi, 1
imul eax, edi
ret
the difference between unsequenced and indeterminately sequenced
;asm cclang_trunk -O3
int main()
{
int x = 1;
return ++x * ++x;
}
main: # @main
mov eax, 6
ret
oh well
yeah ofc
clang is normally pretty aggressive with treating ub as unreachable so I'd have expected it to do that here