This question already has answers here:
Has C++ standard changed with respect to the use of indeterminate values and undefined behavior in C++14?
(1 answer)
Does initialization entail lvalue-to-rvalue conversion? Is `int x = x;` UB?
(4 answers)
Closed 6 years ago.
I am confused about the following code:
#include <iostream>
int i = 1;
int main()
{
int i = i;
std::cout << "i: " << i << "\n";
return 0;
}
Output:
i: 0
I had expected running the above code would print 1. Can someone please explain the reason for this strange behavior?
You are initializing i with itself. The both i's in int i = i; are the inner one not the outer one. This is undefined behavior and you may get 0 or anything may happen.
This is the right way if you want to assign the outer i to the inner i.
#include <iostream>
int i = 1;
int main()
{
int i = ::i;
std::cout << "i: " << i << "\n";
return 0;
}
Live Demo
BTW, You should carefully read all the compiler warnings. If you did you could see the problem yourself:
warning 'i' is used uninitialized in this function
Related
This question already has answers here:
Undefined, unspecified and implementation-defined behavior
(9 answers)
Closed 1 year ago.
#include <iostream>
using namespace std;
int main() {
// your code goes here
string g = "12345";
cout << g[10] << endl; // Prints an empty character
return 0;
}
Reference: https://ideone.com/rpCWm2
This surprisingly works and doesn't throw an error! Can somebody explain how this is happening? Thank you!
std::string g = "12345";
std::cout << g[10] << std::endl;
Your access is out-of-bounds, which makes it an undefined behaviour. Undefined behaviour can do anything, from running normally to crashing the program.
If you use the at() function instead(which does bound-checking), you will see that this will throw an std::out_of_range exception.
std::string g = "12345";
std::cout << g.at(10) << std::endl;
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
How is the postfix and prefix increment operator evaluated in an expression? [duplicate]
(1 answer)
Closed 5 years ago.
I thought I understand how postfix and prefix works until I encounter this
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) {
int a = 3, b = 3, c = 3;
cout << a + a++ << endl; // = 7 Okay, makes sense to me
cout << b + c++ << endl; // = 6 Now I'm confused
int x = 3, y = 3;
cout << (x++)*(x++)*(x++) << endl; // = 3*4*5 = 60 Okay I get it
cout << (++y)*(++y)*(++y) << endl; // = 5*5*6 = 150 Wait what ? But why... ?
// I thought it would be 4*5*6 or 6*6*6 or something
return 0;
}
Is there any logical explaination for this ? Or it's some kind of undefined behaviour ?
C++ does not provide guarantees wich argument will be produced first.
When you code something like:
a + a++
it will be translated to
int operator+(int a1, int a2)
before a call, this function a2 and a1 must be calculated. You can't determinate which parameter will be performed fist. So, you have undefined behavior.
This question already has answers here:
Unexpected order of evaluation (compiler bug?) [duplicate]
(3 answers)
Undefined behavior and sequence points
(5 answers)
Closed 6 years ago.
I came across this piece of code:
#include <iostream>
int main()
{
int m = 44;
std::cout << "m = " << m << ", m++ = " << m++ << ", ++m = " << ++m <<
std::endl;
return 0;
}
My first thought was, this is undefined behavior, but it turns out this code was part of an exam in my university, so I'm not so sure now.
The question on the exam is: what is the output of this program?
I also heard that C++11 or C++14 changed the rules about UB in some way, so it might be defined now.
Anyway, the output is (on gcc in windows)
m = 46, m++ = 45, ++m = 46
Is that output correct?
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the correct answer for cout << c++ << c;?
I just ouputted text, when I suddenly noticed.
#include <iostream>
int main()
{
int array[] = {1,2,3,4};
int *p = array;
std::cout << *p << "___" << *(p++) << "\n";
// output is 1__1. Strange, but I used brackets! it should be at
// first incremented, not clear.
p = array;
std::cout << *p << "___" << *(++p) << "\n";
// output is 2_2 fine, why first number was affected? I didn't intend
// to increment it, but it was incremented
p=array;
std::cout << *p << "___" << *(p + 1) << "\n";
// output is 1_2 - as it was expected
p = array;
return 0;
}
Such behaviour is strange for me, why is it so?
You are causing undefined behaviour, so anything can happen and there's no point in speculating about why.
The expression
std::cout<<*p<<"___"<<*(p++)<<"\n"
Is one example: the order of evaluation of all the things between << is unspecified, so *p and *(p++) are unsequenced with respect to each other (i.e. the compiler is not required do do either one first). You are not allowed to modify a variable and then use it without the modification and usage being sequenced, and so this causes undefined behaviour.
The same thing applies to all the other places in that program where a variable is modified and used unsequenced separately in the same expression.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Undefined Behavior and Sequence Points
The variable i is changed twice, but is the next example going to cause an undefined behaviour?
#include <iostream>
int main()
{
int i = 5;
std::cout << "before i=" << i << std::endl;
++ i %= 4;
std::cout << "after i=" << i << std::endl;
}
The output I get is :
before i=5
after i=2
Yes, it's undefined. There is no sequence point on assignment, % or ++ And you cannot change a variable more than once within a sequence point.
A compiler could evaluate this as:
++i;
i = i % 4;
or
i = i % 4;
++i;
(or something else)