This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
order of evaluation of operands
(6 answers)
Closed 9 years ago.
The very amazing and shocking logical problem occur which simple coding in c++.
See the following two chunk of code.
code 1
int m = 5, n = 0;
n = m++ * ++m;
//This print m = 7 and n = 36
//Which is logically wrong
code 2
int m = 5;
int n = m++ * ++m;
//This print m = 7 and n = 35
//Which is logically right
As we think logically the code block 2 gives right answer, but the amazing or magic thing is that what is wrong with code block1?
As part code its same, just we declared int n earlier.
May be some compile!!!!!!
Check http://en.cppreference.com/w/cpp/language/eval_order especially the part which discusses "Undefined behavior". Basically it's not a bug in the compiler. The language says what you're doing is undefined.
Besides the "undefined behavior" part, which is important, there's nothing illogical here. Assuming the evaluation is from right to left:
++m -> value is 6, m is 6
m++ -> value is 6, m is 7
6*6 = 36
Related
This question already has answers here:
Pre vs Post Increment
(3 answers)
Closed 8 months ago.
I have the following c++ program:
#include <iostream>
using namespace std;
//looping through arrays backwards
int main() {
int a[3] {1, 2, 3};
int x = sizeof(a), y = sizeof(int), z = x / y;
for(int i = z - 1; i >= 0; i--) {
cout << a[i] << " ";
}
return 0;
}
And it outputs 3 2 1. But if I change the first parameter in the for loop to int i = z--;, it outpus 2 3 2 1 and I don't understand why. Aren't z - 1 and z-- supposed to be the same thing? Could someone please explain why? Also, I'm a begginer in C++ and I'm learning via the W3Schools tutorial about it. Thanks!
The expression z-- evaluates to z, then - as a side effect - z is decremented (scheduled according to scheduling rules). This means, you're essentially saying int i = z in your loop (and then decrement z, but it's not used anymore) - therefore, your code has UB. The 2 printed is purely coincidental, anything might be printed or anything could happen in your code. If you'd like to use --, use it as prefix, i. e., int i = --z.
This question already has answers here:
Accessing an array out of bounds gives no error, why?
(18 answers)
Closed 6 years ago.
#include <iostream>
using namespace std;
int main(){
int a[3], no;
cout << "Index Value\n";
for(int i = 0; i < 100; i++){
cin >> no;
a[i] = no;
cout << i << "\t" << a[i] << endl;
}
return 0;
}
Here I initialized a[ 3 ]. In for loop, I'm feeding input 100 times to a[ ], exceeding the indices of [ 3 ].
Why don't it give segmentation error right after when i equals 4.
Input
1 2 3 4 5 6 7
Output
Index Value
0 1
1 2
2 3
4 0
5 5
6 6
7 7
Output is wrong when Index equals 4. Printed 0 . Expected 4
Unfortunately for the debugging programmer, C and C++ programs don't usually segfault when you write past the end of an array. Instead it will usually silently write over whatever the pointer arithmetic as up to -- if the OS allows it. This often overwrites other variables or even program code, causing confusing and unpredictable errors.
I have used the word "usually" here because according to the standards this is "undefined behaviour" -- that is, the compiler and runtime can do anything they like.
When developing and testing, it can be very useful to use a library such as electricfence, which puts extra checks into memory operations and would make your program fail in the way you expect.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
I've seen an interesting statement today with post-increment and pre-increment. Please consider the following program-
#include <stdio.h>
int main(){
int x, z;
x = 5;
z = x++ - 5; // increase the value of x after the statement completed.
printf("%d\n", z); // So the value here is 0. Simple.
x = 5;
z = 5 - ++x; // increase the value of x before the statement completed.
printf("%d\n", z); // So the value is -1.
// But, for these lines below..
x = 5;
z = x++ - ++x; // **The interesting statement
printf("%d\n", z); // It prints 0
return 0;
}
What's going on there actually in that interesting statement? The post-increment is supposed to increase the value of x after the statement completed. Then the value of first x is remain 5 for that statement. And in case of pre-increment, the value of second x should be 6 or 7 (not sure).
Why does it gives a value of 0 to z? Was it 5 - 5 or 6 - 6? Please explain.
It's Undefined Behavior. The compiler is free to do whatever it wants -- it may give 0, it may give 42, it may erase your hard drive, or it may cause demons to fly out of your nose. All of those behaviors are permitted by the C and C++ language standards.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
What is the way of working out:
int i=5;
a= ++i + ++i + ++i;
If we go by normal right to left evaluation, the result should be 21 (6 + 7 + 8) .
I remember studying in school, that the answer was 24(8 + 8 + 8)
But I tried it on CodeBlocks, www.ideone.com, ie gcc 4.8.1 compiler and now I get 22.
Could someone please explain the reason
There are no Sequence Points in that expression, so you can't work it out, it is undefined/compiler dependant.
It is as defined by the C/C++ standards an undefined or implementation-defined behavior. The language standards do not specify the behavior, it allows the compiler implementation to choose (and is usually documented).
Also look at another StackOverflow question: Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…).
What GCC does in your example is the following:
int i = 5;
a = ++i + (++i + ++i); // it first gets the value of i and increments it two times
a = ++i + (7 + 7); // it then reads the new value of i, does the addition and save it as a constant in the stack
a = ++i + 14; // it then gets the value of i and increments it
a = 8 + 14; // it then reads the new value of i and add it to the constant
a = 22;
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
Why this code is generating 8 as a result ?
#include <iostream>
using namespace std ;
void myFunction(int i)
{
i = i + 2 + ++i;
cout<<i<<endl;
}
void main ()
{
int i = 2;
myFunction(i);
cin>> i;
}
I think the result should be 7 not 8...I am using Visual Studio 2008
The order of evaluation of terms on the right hand side of this expression
i = i + 2 + ++i;
is undefined. i.e. they can occur in any order. In this case the compiler has chosen to increment i first (++i, third term), before evaluating i (first term), which results in 3 + 2 + 3.
You are changing i twice in one statement, and also referencing its value in a way not connected to changing it. This is undefined behavior, and there is no single right answer.
Unspecified behavior. It could be any value. You're not allowed to modify a variable more than once in a single sequence point.
The ++i is executed before all other statements are, so in the line i + 2 + ++i the result is (with i=2) 3 + 2 + 3 which is 8.
It's evaluating "++i" first. "i" is then 3 so you end up with 3 + 2 + 3 = 8.
This is an excellent example of why you should be careful with operators!