what is the order of calculating i+v[i++] [duplicate] - c++

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 4 years ago.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v{1,2,3,4,5};
int i = 0;
while (i < 5) {
cout << i+v[i++] << endl;
}
return 0;
}
Why the output of the program is "2,4,6,8,10", instead of "1,3,5,7,9"?

This is undefined behaviour because the read of the "first" i and the other i++ are unsequenced. The output is meaningless.
Further reading: Undefined behavior and sequence points,

Some compilers will warn you that you are doing something that leads to undefined behavior: Relying on the evaluation order of unsequenced expressions.
Example:
<source>:22:22: warning: unsequenced modification and access to 'i' [-Wunsequenced]
cout << i+v[i++] << endl;
The evaluation order of expressions between sequence points is not defined. The only sequence point in the expression i+v[i++] is at the end of the expression, so the compiler is free to order the addition of i and the increment of i however it wants.

Related

Why is *a++; not incrementing the value a points to? [duplicate]

This question already has answers here:
Does *p++ increment after dereferencing? [duplicate]
(5 answers)
Closed 6 months ago.
Hellow
I am a beginner in coding who is learning pointer. this is a simple code of passing pointers
to functions but I am getting wrong input.
#include<iostream>
using namespace std;
void increment(int *a){
*a++;
}
int main(){
int a=2;
//int *p=&a;
increment(&a);
cout<<a;
return 0;
}
output is showing 2;
What happened here? The C++ grammar effectively defines something we commonly refer to as "operator precedence" (since it's defined by the grammar, you won't find chapter in the C++ standard where the precedence would be listed in a convenient way, but there are websites that worked it out).
In *a++;, the pointer will be increased (a++) and thus pointing to an invalid position. After that, the invalid pointer will be dereferenced (* operator), causing undefined behavior (UB). If you're learning, you want to get familiar with undefined behavior, because anything can happen.
To fix it, use parentheses to specify precedence explicity: (*a)++;.
Maybe you want to learn about references instead? Don't use raw pointers and pointer arithmetic. Compare your code to this code:
#include<iostream>
using namespace std;
void increment(int& a){
a++;
}
int main(){
int a = 2;
increment(a);
cout << a;
return 0;
}
Also: Why is "using namespace std;" considered bad practice?

Absurd output. Gives different output with and w/o debugging. Need expert intervention [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 5 years ago.
So here's the question. Find the output for the following code.
perform in c++
#include<iostream>
using namespace std;
int main()
{
int i=2;
cout<<i++<<i<<i++<<i;
cout<<i;
}
This concept is based on post/pre increment operator. Based on this concept I predicted the output as 23344. Like I expected it was correct when I tried debugging this code. But without debugging i am getting the output as 34244.
Is this even possible? BTW I tried this on Dev-C++ 5.11. Thanks :)
Your construction invokes Undefined Behavior.
See Undefined behavior in c/c++: i++ + ++i vs ++i + i++ and Why are these constructs (using ++) undefined behavior?
#include<iostream>
using namespace std;
int main()
{
int i=2;
//cout<<i++<<i<<i++<<i; // UB!
cout<<i++;
cout<<i;
cout<<i++;
cout<<i;
return 0;
}

semicolon after the for loop block [duplicate]

This question already has answers here:
Semicolon at the ends of if-statements and functions in C
(7 answers)
Closed 5 years ago.
#include <iostream>
int main() {
for( int i = 0; i < 5; ++i) {
std::cout << "Hello" << std::endl;
};
}
Is there any case that the semicolon after the for loop would affect the program ?
The semicolon is an empty expression statement.
From section 6.2 of the C++ standard
The expression is a discarded-value expression (Clause 5). All side
effects from an expression statement are completed before the next
statement is executed. An expression statement with the expression
missing is called a null statement. [ Note: Most statements are
expression statements — usually assignments or function calls. A null
statement is useful to carry a label just before the } of a compound
statement and to supply a null body to an iteration statement such as
a while statement (6.5.1). —end note ]
This will be more clear with some reformatting:
#include <iostream>
int main(){
for(int i=0; i<5; ++i){
std::cout <<"Hello"<<std::endl;
}
;
}
The presence of this null statement has no effect on the program.
No.
The semicolon is not even "attached" to the loop; it's just an empty statement sitting there, effectively on its own.
It doesn't change anything. It just evaluates to an empty statement.
It's completely harmless. Just a bit of pointless clutter.

c++ argument evaluation order for assignment? [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
In c++, arguments evaluation of order is not guaranteed, but is the order of left/right sub expression of assignment expression is guaranteed? For example
#include <iostream>
#include <map>
int main()
{
int i = 2;
std::map<int, int> map;
map[i++] = i--;
return 0;
}
Is left expression i++ guaranteed to be executed before right expression i--?
You asked:
Is left expression i++ guaranteed to be executed before right expression i--?
No, it is not.
The line
map[i++] = i--;
could end up being
map[2] = 3;
or
map[1] = 2;
depending on which side of the assignment operator gets evaluated first.
However, since the line invokes undefined behavior, it could also be, as pointed out in the comment by #juanchopanza, :
map[42] = -999;

printf("%d %d\n",i++,i++) output [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 9 years ago.
Why does this code shows the output as "3 2" instead of "2 3" ?
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<map>
#include<vector>
using namespace std;
int main()
{
int i=2;// declare
printf("%d %d\n",i++,i++);//print
return 0;
}
Output is : "3 2"
Why it prints in reverse order
In this statement, the expression
"printf(...)" modifies the variable "i" more than once
without an intervening sequence point.
This behavior
is undefined.
The compiler has detect a case where
the same variable has been modified more than once in
an expression without a sequence point between the
modifications. Because what modification will occur
last is not defined, this expression might produce
different results on different platforms.
Rewrite the expression so that each
variable is modified only once.
even you might get output "2 3" in different compiler
The order of evaluation of printf is from right to left in here
First evaluate
printf("%d %d\n",i++,i++);
^
Then
printf("%d %d\n",i++,i++);
^
So you got the output as 3 2
The behaviour will be definitely undefined due to the undefined evaluation order of parameters.
Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.