This causes undefined behavior. Undefined behavior means that everything can happen, including if it works. The temporary unique_ptr actually destroyed and as a result frees up a 100-element char array. You read and write to a memory location that is no longer allocated to you.
It so happened that the memory indicated by x was not allocated or read / written for something else by the time you work with it. But this memory has already been freed by the temporary unique_ptr , so you shouldn't mess with it.
Just don't do it. If you want to keep the array, but not unique_ptr , use release() .
source share