+ 1

I am getting this weird result??

During experiments with increment operator, I got this weird result. How it's happening? It should come Zero. isn't it ?? https://code.sololearn.com/cJr9XtYXxn2e/?ref=app

18th Mar 2022, 3:53 AM
NonStop CODING
NonStop CODING - avatar
11 Respostas
+ 7
It is undefined behavior to use the same variable again in the same expression as a ++ or -- operator. This applies to your code but also to function calls like fnc( c, c++ ) or fnc( c++, c-- ). If you're gonna use ++ or -- operators never use the same variable again in the same expression. It's not a precedence issue. The way the compiler compiles the code can differ depending on the way the compiler is implemented. For example: (the latest version of) gcc will output 1 ( sololearn uses this compiler ) clang and msvc will output 0. The way gcc compiles this code like this ( unoptimized ): ( cpu registers are the very fast internal memory storage inside the cpu ) - Move the value 1 into memory location a ( the c variable ) mem a now contains the value 1 - Move the memory location a into the cpu register eax eax now contains the value 1 - Increment eax by 1 and store it in the cpu register edx edx now contains the value 2 and eax 1 ( eax itself wasn't incremented ) - Store edx into the memory location a this doesn't do anything, but memory a now contains the value 2 - Now finally subtract edx from eax edx = 2, eax = 1. 2 - 1 = 1 Now lets see how clang does it: - Store the variable 0 into memory location a for whatever reason - Store the variable 1 into memory location b mem b = 1 - Move the value in memory location b into the cpu registers eax, ecx and edx eax = 1, ecx = 1, edx = 1 - Add 1 to edx eax = 1, ecx = 1, edx = 2 - Move the value in edx back to memory location b mem b = 2 - Finally subtract ecx and eax ecx = 1, eax = 1 so 1 - 1 = 0 clang also emits the warning: warning: unsequenced modification and access to 'c' [-Wunsequenced] cout << c - c++;
18th Mar 2022, 12:13 PM
Dennis
Dennis - avatar
+ 4
Good question, where did I learn about it? I kinda forgot. I was interested in learning the assembly language so you just run into those things like cpu registers, memory and calling conventions naturally because you have to know them in order to work with that language. Then I implemented a gameboy emulator and a cpu emulator ( the latter should be in my codes somewhere ). A great website you can use to see what your program is actually doing under the hood is from this website: https://godbolt.org/ I recommend you to type some simple code ( like your program above ) and try to understand each and every instruction through google and try to do it yourself on paper ( or notepad ) to see if you really understand what it's doing. For example you'll regularly see functions starting with: ( e = 32 bit register, r = 64 bit register, no e nor r = 16 bit register ) push rbp mov rbp, rsp possibly sub rsp, n and ending with pop rbp ret Through some googling you'll figure out that the program is setting up a stack frame: https://en.m.wikibooks.org/wiki/X86_Disassembly/Functions_and_Stack_Frames You just gotta play with things like that to get an understanding of it, and aloooot of googling.
18th Mar 2022, 12:31 PM
Dennis
Dennis - avatar
+ 3
G'day NonStop CODING did you skip over post-increment and pre-increment lessons? When you use var++ the increment happens after the line gets executed. When you use ++var it gets incremented before the line gets executed. Same scenarios for var-- and --var. {edit: hold up, you are doing c - c++ that should be 1 - 1 then increment after execution. hmmm ....} Edit2: if you change it to cout << c - ++c; the output is then 0 you said it doesn't matter what C is holding, eg C=42 still comes out as 1. Maybe it is a true/falsey sort of thing?
18th Mar 2022, 4:02 AM
HungryTradie
HungryTradie - avatar
+ 3
HungryTradie please Read Thankyou very much for giving your time to explain. 👍 but i knew about post increment thing. i just decided to test out different possible ways you can use this. What will happen if i write like this?? That's why i wrote the expression like that. But The main reason i posted this because i thought before writing the code that what will happen if we write x - x++. So i thought first that it will come 0 because the value of x will be used first then it will get incremented. (for a beginner it's confusing isn't it?) But when i wrote this it came 1, This was weird. So after that i looked it carefully and it reminded me of C (language) - C++ ( language ) that's why i wrote the file name as "difference between C and C++ " it's 1 😁😁😁. Looked at the file name If somebody asked me whats the difference between C and C++ then i'm going to say it's 1. is it correct ? and if you know how it came 1 then please tell me. It's getting confusing. i dont want to feel like a Stupid👍
18th Mar 2022, 4:46 AM
NonStop CODING
NonStop CODING - avatar
+ 2
You were also using the variable without assigning it a value, I gave it 42. I then thought "I bet it is an undefined behaviour warning because you are manipulating the variable as well as using it. You are using the variable in the same statement that it post-increments. This causes the compiler to show a warning. Change to this and get no warnings: int C=42; //int C = 1; the result is same always no matter what the value of C is int otherC=C; cout << (otherC - C++); {Edit: did you miss the strong suggestion to use capital letters for constants and defined terms, use lowercase for variables?}
18th Mar 2022, 4:12 AM
HungryTradie
HungryTradie - avatar
+ 2
Dennis Aah! Thankooo vary much😘😘 where did you read about all these things? Complete internal working. Please tell me if the source name.
18th Mar 2022, 12:21 PM
NonStop CODING
NonStop CODING - avatar
+ 2
HungryTradie thankoooo tooo😚😚
18th Mar 2022, 12:22 PM
NonStop CODING
NonStop CODING - avatar
+ 1
HungryTradie I think i got it how it's working. i checked the precedence. so this is how it is. let int C = 1 ( or anything else or even undefined(garbage) does not matter ) let C - C++ ( left - right) first C++ will be evaluated. As we know that the value of C will be taken first then it will get incremented. it means the "right" will be equal to 1 ( it's like saying 1++) after incrementation C will become 2 ( which is actually the left side ) so it's like saying 2 - 1++ ( ++ will not be here it's just for understanding purpose) so this is how it will get evaluated i guess. So answer is always going to be 1. No matter what the value of C is. please correct me if I'm wrong.
18th Mar 2022, 5:15 AM
NonStop CODING
NonStop CODING - avatar
+ 1
Oh, that seems very close. What probably happens is 1 - 1 gets evaluated as 0, and then the variable gets incremented.
18th Mar 2022, 7:10 AM
HungryTradie
HungryTradie - avatar
+ 1
i saw that ++ has higher precedence than - how can minus takes place before ++ ? i just follwed the precedence. it looks like i have to memorize these kinds of things. maybe this is why python does not have ++ and -- operator. Because it has weird behaviour.
18th Mar 2022, 10:50 AM
NonStop CODING
NonStop CODING - avatar
0
First of all the script if your project is incorrect . It has many mistakes please view your syntax errors with the warning in your output message me if you need any help until then goodbye , goodluck
18th Mar 2022, 12:39 PM
Hrithika
Hrithika - avatar