This question already has answers here:
Undefined behavior and sequence points
(5 answers)
pre and post increment operations on a variable give different output on TC and gcc [duplicate]
(5 answers)
Closed 9 years ago.
int a = 0;
cout << a << a+1 << a << 1+a << a;
// output is 01010 , ok fine i understand it! :-)
cout << endl;
cout << a << ++a << a << a++ << a;
// output is 22202 ,
// Plzzz help me how compiler interprets my this statement
// i cant understand :-(
int x = 1;
cout << ++x + ++x;
// output is 6
// how ??
Please if anyone could explain it to me how these outputs are coming :-)
Thanks in Advance!
It depends on the type of x. If x is a built-in type (e.g., int) then you have undefined behavior because you're modifying x twice without an intervening sequence point1.
If x is a user-defined type, then you have unspecified behavior.
1. Technically C++11 has change the terminology so "sequence point" is no longer used, bu the effect is the same.
Related
This question already has answers here:
Accessing an array out of bounds gives no error, why?
(18 answers)
uint8_t can't be printed with cout
(8 answers)
Closed 1 year ago.
this is probably a really basic C++ question but I'm very new to C++ so your patience and help is appreciated.
I'm not quite sure why the following code is unable to print out the content of the matrix. I should mention that the variable K is the integer 3.
std::vector<uint8_t> enc_matrix;
for(size_t i = 0; i != K; ++i) {
enc_matrix[i*(K+1)] = 1;
cout << "enc_matrix: " << enc_matrix[i*(K+1)] << endl;
// cout << enc_matrix.at(i*(K+1)) //doesn't show anything either
cout << "i: " << i*(K+1) << endl;
}
For whatever reason nothing gets printed for enc_matrix[i*(K+1)] but the result of i*(K+1) does get printed. Below is what gets printed:
enc_matrx:
i: 0
enc_matrx:
i: 4
enc_matrx:
i: 8
I'm not sure if it has anything to do with the type of the matrix so I've included the additional info below:
typeid(enc_matrix[0]).name() => h and
typeid(enc_matrix).name() => St6vectorIhSaIhEE
Why is it that enc_matrix doesn't get printed? I am expecting to see 1 assigned to it when i equals to 0, 4 and 8. Thank you.
This question already has answers here:
How does the Comma Operator work
(9 answers)
Closed 6 years ago.
I am trying to assign part of a QVector double array to complex double array in Qt.
Running the following code, I have the problem that std::cout << y.at(i) << std::endl; correctly prints a 16-bit integer value but std::cout << y1[i] << std::endl; prints zeros: (0,0) (0,0) (0,0) ...
I would like the result (y[0],0) (y[1],0) (y[2],0)...
Please can someone advise what is wrong?
QVector<double> x(100), y(100); // Original QVector
std::complex<double> y1[100];
int i=0;
while(i<101)
{
// get y values from somewhere else (quint8 data8)
y[i] = (256.0*data8[2*i]) + 1.0*data8[2*i+1];
std::cout << y.at(i) << std::endl;
y1[i] = (y.at(i),0.0);
std::cout << y1[i] << std::endl;
i++;
}
Let's look at this expression:
(y.at(i), 0.0)
In C++ parentheses are just a grouping technique, which is used for operator precedence designation. For example, if you execute 2*2+2, you'll get six. But if you want to get eight, you need parentheses: 2*(2+2). So you assignment line is equivalent to:
y1[i] = y.at(i), 0.0;
Why does it compile and what does it mean? It compiles because a comma operator is used. In C++, comma is a binary operator with a very simple action: it computes it's left-hand argument, throws the result away, then computes it's right-hand argument and returns the result. So in your case it discarded y.at(i) and returned zero, which was successfully assigned to y1[i]. The shortest way to do what you want in modern C++ is to use initializer lists:
y1[i] = {y.at(i),0.0};
If you have an ancient compiler, it's just a bit more verbose:
y1[i] = std::complex<double>(y.at(i), 0.0);
This question already has answers here:
Cannot understand for loop with two variables [duplicate]
(3 answers)
Closed 6 years ago.
This compiles and runs, but produces garbage values for "a". Why doesn't "a" increment like "b"? Why is it producing garbage?
for(a,b=0; a,b != 55; a,b++)
{
//outputs garbage
std::cout << "a = " << a << std::endl;
//outputs expected results
std::cout << "b = " << b << std::endl;
}
The comma operator says execute the expression on the left then execute the expression on the right:
a, b=0
first executes a which does nothing, then it executes b=0 which assigns zero to b.
Why does the comma operator exist? The comma operator can be useful when the expressions have side effects.
It also serves a sequence point which tell the compiler "everything on the left must be complete before anything on the right happens. This constrains the optimizations allowed by the compiler, so for example a += 1, b = a + c[a] will always add one to a before using it as an index. Something like b = ++a + c[a] is undefined because the compiler can increment a before or after it uses it as an index.
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
The following code gives a weird output. I have tried it on multiple compilers and ended up with the same answer. It will process the statement right to left but print the output left to right however c++ statements are evaulated left to right in general. Can someone explain why this happens when we overload the cout statement.
Output:
15
10
5
However the Output if processed from left to right should be:
8
10
12
#include<iostream>
using namespace std;
int main(){
int a = 5, b = 3, c = 2;
cout<< (a = b + a) << endl << (b = c + a) << endl << (c = b + c);
return 0;
}
This will result in unspecified behavior. The order in which the operations execute isn't specified (since there are no sequence points between them), so the three assignments
a = b + a
b = c + a
c = b + c
can occur in any order. Therefore the output from cout is also unspecified .
This question already has answers here:
What is the correct answer for cout << a++ << a;?
(4 answers)
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
I Cant really undersdand suffix. I know it first uses identifier and then increases or decreses , as first shows i and then ++. But now i think im wrong and still don't understand it.
#include <iostream>
using namespace std;
int main()
{
int i = 0;
cout << i << i++ << i;
cout << "\n\n\nPress Enter to close the window . . . ";
cin.clear();
cin.sync();
cin.get();
return 0;
}
Output:
101
Press Enter to close the window . . .
first i is changed before increment readed.Why?
I expected
001
Press Enter to close the window . . .
Can someone explain.
Just never do such a thing, it is undefined
cout << i << i++ << i;
better do
cout << i << i << (i + 1);
i ++;
if you want your expected result.
The case
cout << i++;
is defined and perfectly ok.
I think what is undefined*) here is the order of evaluation of function arguments. What you are actually calling here are function calls to the (overloaded)
std::ostream& operator<< (std::ostream&, int);
and the first argument is the output of another call to the same function, so your
cout << i << i++ << i;
expands to
operator<<( operator<<( operator<<(cout,i), i++), i);
As the order in which function arguments are evaluated is not specified, anything can happen here. You can avoid that by writing separate lines:
cout << i;
cout << i++;
cout << i;
which expands into the harmless
operator<<(cout,i);
operator<<(cout,i++);
operator<<(cout,i);
*) edit: to be more precise, the cout<<i<<i++; is undefined because the order of evaluation of function arguments in unspecified.