The program invokes undefined behavior. When a program does this, the compiler can do whatever it wants, including creating a program that works as you would expect.
The likely reason why he works the way he does is as follows. At the end of the inner block, test goes beyond and its destructor is launched. This frees up the block of memory used to store the actual string for other purposes, but the memory is not cleared (this will be a waste of time). Memory allocated in this way is not reused until bbbb one reason or another.
(Please note that the purpose and seal of cccc valid.)
source share