#I cant seem to understand how this code works, I know it might be really easy but I'm kinda a noob

106 messages ยท Page 1 of 1 (latest)

silver prairieBOT
#

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 Do Not Delete Posts!

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.

torn condor
#

#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;
}

radiant scroll
#

this code has undefined behavior.

torn condor
#

wdym?

torn condor
arctic hatch
#

y = x++ * ++x;
stuff like this is also just not great

torn condor
#

no i added it in a c++ ide it gives answer 749735

arctic hatch
torn condor
#

i'm really sorry but i'm really a beginner at this

#

i really dont understand

arctic hatch
#

undefined means that the result could be whatever

#

or atleast are undetermanistic

torn condor
#

yh but take for example this line

#

int x, y;
x = 5;
y = ++x * ++x;

#

what should the value of y be?

arctic hatch
#

I believe that one is mostly fine, it's the one later that's problematic

torn condor
#

yh but what could y be?

arctic hatch
#

afaik this one is just 6 * 7 or 7 *6

torn condor
#

yh but it gives 49 as answer

arctic hatch
#

;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;
}
leaden spearBOT
#
Program Output
X:7 Y:49
X:7 Y:35
torn condor
#

you see

arctic hatch
#

the result isn't 749735.....

torn condor
#

no but for the first part x becomes 7 and y becomes 49 ryt?

#

you said 42 i thought 42 too

arctic hatch
#

๐Ÿค” 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

torn condor
#

could you explain it to me please?

#

@arctic hatch you there?

arctic hatch
#

The simplest solution is simply don't write confusing code like this and it's easy to reason about

radiant scroll
#

the behavior of this code is undefined.

torn condor
#

its a question i got as homework as an mcq

arctic hatch
#

Then just awnsers the end result is UB :P

torn condor
#

what kind of reply is this

arctic hatch
#

I didn't mean with that you wrote it

torn condor
#

so you dont know?

arctic hatch
#

No I'm saying you can't know

#

Cause it's undefined behavior

torn condor
#

bruv

arctic hatch
#

But all I was saying with that is by simply not writing confusing code like this you solve the problem of ambiguity

torn condor
#

i'm saying why did y have a value of 49 instead of 42

arctic hatch
#

Where is this hostility coming from

torn condor
#

i'm asking you a question you're not answering properly

arctic hatch
torn condor
#

how tf

#

what about the prefix increment

#

why did it become 49

radiant scroll
#

again, this code has undefined behavior

torn condor
#

x = 5;
y = ++x * ++x;

#

look at this part

arctic hatch
#

Because it propably evaluated x to be 7

arctic hatch
torn condor
#

what value should y be

radiant scroll
#

it's undefined

arctic hatch
torn condor
radiant scroll
#

because the rules of C++ say so

arctic hatch
# radiant scroll it's undefined

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?

torn condor
#

i have an mcq question telling me this lines of codes and has choices

#

you want me to say undefined??

arctic hatch
brazen bear
torn condor
#

wait but hasn't x been assigned a value of 5

#

y should computate using that value ryt?

brazen bear
#

no...

torn condor
radiant scroll
# torn condor x = 5; y = ++x * ++x;

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.

torn condor
#

yh but when compiled it gave an answer

arctic hatch
#

Say you have a million options when you compile it gives you one of them

silver prairieBOT
#
Undefined Behavior

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.

Example: Read Indeterminate Val.
int i; // default init,
       // indeterminate value
while(i < 10) {
    printf("%d\n", i++);
}
Example: Out-of-Bounds Access
int arr[10];
for(int i = 0; i < 20; i++) {
    arr[i] = i; // [i] out of
                // bounds
}
Consequences of UB

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.

See Also

Sanitizers can help identify UB. For more info, look into address sanitizer (ASan) and undefined behavior sanitizer (UBSan). See !howto asan

arctic hatch
#

It seems like the compiler ended up making it 7*7

brazen bear
# torn condor why

++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

torn condor
#

ah ok

arctic hatch
#

So afaik it can be, 7x7, 6x7 or 7x6

#

Hence why I said simply don't do this

azure nimbus
#

Or it can print "Lol, fix your program bro"

torn condor
#

ok i'm getting it now

radiant scroll
#

it's ub, so it doesn't have to be anything.

#

the compiler could also just optimize away your whole code

arctic hatch
torn condor
#

๐Ÿ™ really sorry for being aggressive and insulting you

brazen bear
torn condor
#

i'm really sorry i'm new at c++ and really confused

arctic hatch
#

Yeah all good

#

But yeah the entire reason I said don't so this, is cause stuff like this becomes a minefield

torn condor
#

ok

arctic hatch
radiant scroll
#

it could also be 42ร—-1

radiant scroll
brazen bear
#

;asm -O

int a (int x) {
  return ++x * ++x;
}
int b (int x) {
  auto inx = [&] { return ++x; };
  return inx() * inx();
}
leaden spearBOT
#
Assembly Output
a(int):
  lea eax, [rdi+2]
  imul eax, eax
  ret
b(int):
  lea eax, [rdi+2]
  add edi, 1
  imul eax, edi
  ret

brazen bear
#

the difference between unsequenced and indeterminately sequenced

radiant scroll
#

;asm cclang_trunk -O3

int main()
{
    int x = 1;
    return ++x * ++x;
}
leaden spearBOT
#
Assembly Output
main: # @main
  mov eax, 6
  ret

brazen bear
#

i think it's still UB if you make it volatile

#

cclang_trunk

radiant scroll
#

oh well

radiant scroll
#

clang is normally pretty aggressive with treating ub as unreachable so I'd have expected it to do that here