Can any one Give the example for
Boolean_conversions Using reinterpret_cast<>
Iam getting the execution failure..Any one explain ..
Ex:
void Boolean_Conversions()
{
bool b = 0;
int *i = reinterpret_cast<int*> (&b);
// iam expecting as *i is 0/false ,but it is showing runtime error
}
Edit:
In ISO Standard
Section $5.2.10, para 1:
"The result of the expression reinterpret_cast<T>(v) is the
result of converting the expression v to type T. If T is a
reference type, the result is an lvalue; otherwise, the re
sult is an rvalue and the l-value to r-value (4.1), array
to pointer (4.2), and function to pointer (4.3) standard c
onversions are performed on the the expression v. Types sh
all not be defined in a reinterpret_cast."
From this Iam trying ..I did many conversions ..like Array_to_Pointer,Function_To_Pointer,Integral/Pointer_Conversions ,etc…But i don’t know why it is failing For Boolean Conversions.
Can any one tell why it is failing ..Otherwise Tell the Boolean COnversions using reinterpret_cast.
Mostly It is Giving Run time failure in G++.CC/EDG/COdepad..etc compilers
Please Explain
No, the output is not defined. Note that the conversion is undefined.
On my machine *i = 0xcccccc00
which corresponds to the value of the bool variable initialized with 0(false). The byte pattern in the other three bytes (assuming the size of int as 32-bits) is completely unspecified and depends on what is there in that memory/endianness etc.
Technically, however that is an undefined behavior because we are deferencing a memory location (all of) which we did not reserve/allocate.