Consider the following code:
#include <iostream>
int main()
{
char* c = new char('a');
char ac[4] = {'a', 'b', 'c', 'd'};
unsigned long long int* u = reinterpret_cast<unsigned long long int*>(c);
unsigned long long int* uc = reinterpret_cast<unsigned long long int*>(&ac[3]);
*u = 42;
*uc = 42;
std::cout<<*u<<" "<<*uc<<std::endl;
}
Is this considered as a valid code, or is it memory leak/undefined behaviour? I am asking, because through:
*u = 42;
*uc = 42;
we are accessing bytes that should not be reachable by the program (I guess).