Both are wrong. The pointer is bnot initialized, so you should not access it through memory.
B *b;
b->val = 1;
So, you were lucky when one of them crashed.
You were unlucky to others, and it did not work.
difficult
You can remove the indirect ...
B b;
b.val = 1;
Or you can highlight it ...
std::unique_ptr<B> b(new B());
b->val = 1;