Let's say there is the following classes:
class A {
// some data here (let's say - 200 bytes)
public:
void foo();
};
class B {
// some data (150 bytes)
public:
void bar();
}
And code:
A *a = new A();
a->foo();
B *b = reinterpret_cast<B*>(a);
// then we just stop using a
a = nullptr;
// and start using this memory as b
b->bar();
Is there an undefined behaviour? (If nowhere else in the code a
is no longer used, and I'm sure that 150 first bytes of this memory is quite valid for object b
).
All i can find is this. And how can I understand, what I'm doing is an undefined behaviour...
Of course, I understand that there may be a loss when deleting such a piece of memory using delete b;
. But can I use this code, or b->bar()
is anyway UB?