Is this code undefined behavior? [duplicate] - c++

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?

Related

Disproving Determinism via c++ [duplicate]

This question already has answers here:
Two different values at the same memory address
(7 answers)
C/C++ changing the value of a const
(18 answers)
Closed 5 months ago.
friends.
Recently I experimented a bit a C++ constants.
The code is:
#include <iostream>
int main() {
const int c = 1;
const int* ptr = &c;
int* tmp = const_cast<int*>(ptr);
*tmp = 5;
std::cout << &c << " " << ptr << " " << tmp << "\n";
std::cout << c << " " << *ptr << " " << *tmp;
}
I have investigated assembly code at godbolt: https://godbolt.org/z/7e3o7bWrs
The assembly code seems like doing what I wrote, exactly moving address of c into tmp variable and changes variable at this address.
Can you please tell me, why there is could be two different values at the same addresses?
Thank you.

Why is the value of i == 0 in this C++ code? [duplicate]

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

C++11: Unsequenced modification and access to variable (streams) [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Unsequenced value computations (a.k.a sequence points)
(2 answers)
Closed 8 years ago.
I recently wanted to get a better feeling for the return values of the ++i and the i++ operator. So I created this small program here:
#include <iostream>
using namespace std;
int main()
{
int i = 0;
int j = 0;
cout << "i++ " << i++ << " i " << i << endl;
cout << "++j " << ++j << " j " << j << endl;
}
Now looking at the code I would expect that when I execute it I get the following result:
i++ 0 i 1
++j 1 j 1
Compiling the above snippet with llvm/clang 3.5 (Apple LLVM version 6.0 (clang-600.0.51)) yields the following warning:
main.cpp:8:23: warning: unsequenced modification and access to 'j'
[-Wunsequenced]
cout << "++j " << ++j << " j " << j << endl;
^ ~
1 warning generated.
What does that mean in this context? The only blog post I found was not so helpful since I can't imagine a reordering of the output somehow.
What would the output of such a reordering look like?
There's also a couple of similar questions here. The only good question on the subject I found was this one. But I don't understand how this would create the compiler warning above.
Compiling the code from above with gcc 4.9 (the flag -std=c++11 was enabled) didn't show this warning at all.

const_cast - Removing the constness of a simple integer. Same memorly location has different values [duplicate]

This question already has answers here:
Strange behavior of const_cast [duplicate]
(4 answers)
How is a variable at the same address producing 2 different values? [duplicate]
(4 answers)
Closed 8 years ago.
I am using const_cast to remove the constness of an integer.
#include<iostream>
int main(){
const int pi = 3;
int & pie = const_cast<int &>(pi);
pie = 4;
std::cout<<pi << " " << &pi << " " << pie << " " << &pie << std::endl;
};
Output:
root#ubuntu-OptiPlex-380:~/cpp# ./a.out
3 0x7fffdbc66f1c 4 0x7fffdbc66f1c
Can anyone kindly explain how the same memory location can hold different values?

Is this example causing an undefined behaviour? [duplicate]

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)