Code:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
//undefined behaviour?
int i = 100;
*(char *)&i = 25;
cout << i << endl;
int &r = *new int;
r = 5;
return 0;
}
Is the first part of the code undefined behaviour?
It seems like it works for any type smaller then an int, but when i try
Code:
int i = 100;
*(float *)&i = 25;
cout << i << endl;
That wont work (compiles but the value is changed to a trash val).
For the second part of the code, with references, what happens?
A compiler creates a temporary variable in which he stores the new int and reference "points" to the temporary variable which is unnamed?
What is the point of such references?
One more thing: is casting away constness undefined behaviour?
With const_cast i mean.
And... why is ~ operator when used on for example int, implementation defined? Is it because it depends if the system it's being used on uses 2s complement system to represent variables?