Simply: If i static_cast
a type X*
to void*
, is it always safe to reinterpret_cast
it back to X*?
I am unable to produce any case where this fails for example:
#include <iostream>
struct a
{
int* m_array;
};
int main()
{
bool fail = false;
for(int i = 0; ++i < 5000;)
{
a* pA = new a;
pA->m_array = new int [i+1]; // A new size of data everytime
pA->m_array[i] = 10;
void* pvA = static_cast<void*>(pA);
pA = reinterpret_cast<a*>(pvA);
if(pA->m_array[i] != 10)
{
fail = true;
break;
}
delete []pA->m_array;
delete pA;
}
if(fail)
std::cout<<"FAILED!!";
else
std::cout<<"Never failed :/";
}
Link to compiled example
Gives the result "Never failed :/" in both debug and release mode with vs 2012. However this is most likely undefined behavior. Right?