C++ strange behaviour of cout's flush. [duplicate] - c++

This question already has answers here:
Order of execution in operator <<
(4 answers)
Closed 6 years ago.
Consider the following code. expected output should be
0 1
1 2
2 3
and so on.
#include<iostream>
using namespace std;
int f=0;
int B()
{
return f;
}
int A()
{
return f++;
}
int main()
{
cout<<A()<<" "<<B()<<endl;
cout<<A()<<" "<<B()<<endl;
cout<<A()<<" "<<B()<<endl;
cout<<A()<<" "<<B()<<endl;
return 0;
}
but the actual output is
0 0
1 1
2 2
and so on.. why?
and if i change code like this-
int main()
{
int f=0;
cout<<f++<<" "<<f<<endl;
cout<<f++<<" "<<f<<endl;
cout<<f++<<" "<<f<<endl;
cout<<f++<<" "<<f<<endl;
return 0;
}
then i get correct expected output
why?

The order of evaluation of the operands of << is not specified. So
cout << A() << " " << B() << endl;
can be treated as either:
temp1 = A();
temp2 = B();
cout << temp1 << " " << temp2 << endl;
or as:
temp2 = B();
temp1 = A();
cout << temp1 << " " << temp2 << endl;
Performing side effects on a variable and accessing it without defined sequencing results in undefined behavior.

Related

pointer to a const variable will produce two different values after changed the value through the pointer [duplicate]

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(int argc, char const *argv[])
{
const int a = 1;
int* b;
b = (int*)&a;
*b = 6;
cout << &a << endl;
cout << b << endl;
cout << a << endl;
cout << *(&a) << endl;
cout << *b << endl;
return 0;
}
The output is as follows:
0x7ffc42aeb464
0x7ffc42aeb464
1
1
6
As the result, the memory address is same but the value is different. What is the reason?
Because you have cast it to non-const in (int*)&a. As I remember, this is undefined behavior.

What happened in the function convert(&m)? [duplicate]

This question already has answers here:
Post-increment and Pre-increment concept?
(14 answers)
Closed 1 year ago.
Here is the code:
int convert(int* a) {
return (*a)++;
}
int main(){
int m = 56;
int n = convert(&m);
cout << m << endl;
m = convert(&m);
cout << m << endl;
return 0;
}
Why is the answer m=57 instead of m=58 after m=convert(&m)?
The second call increments m to 58, but it returns the original value (57) due to the use of the post-increment ++ operator. That original value is then assigned back to m, overwriting the incremented value. The net effect is that m is unchanged.
You can verify this by adding some printouts to see the exact values in play:
int convert(int* a) {
std::cout << "*a was " << *a << std::endl;
int ret = (*a)++;
std::cout << "*a is now " << *a << std::endl;
std::cout << "return value is " << ret << std::endl;
return ret;
}
m = convert(&m); prints:
*a was 57
*a is now 58
return value is 57

When the object is deleted? [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
C++ delete - It deletes my objects but I can still access the data?
(13 answers)
Closed 4 years ago.
I have the following code:
class A
{
public:
int x = 6;
~A() { std::cout << "\ndestr invoked\n"; }
};
int main()
{
int* x;
{
A a;
std::cout << &a.x << "\n";
x = &a.x;
}
std::cout << x << ": " << *x;
}
With output:
0x78cac859fc00
destr invoked
0x78cac859fc00: 6
If I understand correctly it seems that destructor was invoked automatically but in memory variable remained. Does anyone know why?
In the following example (pointer was used so object was deleted manually):
class A
{
public:
int x = 6;
~A() { std::cout << "\ndestr invoked\n"; }
};
int main()
{
int* x;
{
A* a = new A;
std::cout << &a->x << "\n";
x = &a->x;
delete a;
}
std::cout << x << ": " << *x;
}
Variable was cleared:
Output:
0x1381360
destr invoked
0x1381360: 0
Does anyone know what the difference is?

C++ - Copy constructor with pointers as the data field

I have the following code :-
#include <iostream>
using namespace std;
class A {
int *val;
public:
A() { val = new int; *val = 0; }
int get() { return ++(*val); }
};
int main() {
A a,b = a;
A c,d = c;
cout << a.get() << b.get() ;
cout << endl ;
cout << c.get() << endl ;//
cout << d.get() << endl;
return 0;
}
It produces the output as :-
21
1
2
The behavior in case of c.get and d.get is easy to understand.
Both c and d share the same pointer val and a and b share the same pointer val.
So c.get() should return 1 and d.get() should return 2.
But I was expecting similar behavior in a.get() and b.get(). (maybe I have not understood cout properly)
I am unable to understand how a.get() is producing 2.
Can you explain why I am getting such an output. According to me the output should have been :-
12
1
2
cout << a.get() << b.get() ;
gets executed as:
cout.operator<<(a.get()).operator<<(b.get());
In this expression, whether a.get() gets called first or b.get() gets called first is not specified by the language. It is platform dependent.
You can separate them into two statements to make sure they get executed in an expected order.
cout << a.get();
cout << b.get();

exceptions C++ , I just don't understand why the output is what it is

#include <iostream>
using namespace std;
class Array
{
private:
int* a;
int n;
public:
Array(int n) : n(n) {a = new int[n];};
~Array(){delete a;};
int& operator[](int i)
{
try
{
if ( i < 0 || i >= n) throw 1;
return a[i];
}
catch(int i)
{
cout << "Exception Array" << endl;
return a[0];
}
};
};
int main()
{
Array a(2);
try
{
a[0] = 1;
cout << "a[0]=" << a[0] << endl;
a[1] = 1;
cout << "a[1]=" << a[1] << endl;
a[2] = 2;
cout << "a[2]=" << a[2] << endl;
}
catch(int i)
{
cout << "Exception main " << endl;
}
cout << "End. " << endl;
}
Okay, so the output is this:
a[0]=1
a[1]=1
Exception Array
Exception Array
a[2]=2
End.
The thing that is confusing me is the reasoning why the program is returning
a[2] as value 2? Can someone go into more detail as to how this is achieved, step by step. I think I am not understanding something about exceptions in C++.
a[2] = 2;
Here you invoke your operator[] with an out-of-bounds value; it prints Exception Array and returns a reference to a.a[0]. As such (ignoring the log message), this assignment is equivalent to a[0] = 2; - the value of a.a[0] is now 2.
cout << "a[2]=" << a[2] << endl;
Here you again invoke the operator[] with the same parameter; it again outputs the exception message and returns a reference to a.a[0] whose value is still 2, as assigned in the previous statement.
This code
a[2] = 2;
cout << "a[2]=" << a[2] << endl;
tries to access a[2] twice, once in the assignment and once in the output statement. They are both out of range, so two exceptions.