char* pArray = nullptr;
{
char buffer[64];
sprintf_s(buffer,"Time: 123456");
pArray = buffer;
}
cout<<pArray<<endl;
“Time: 123456” is displayed even though the buffer has been deallocated back to the stack. What is going on here? Is this safe? Not safe?
It’s undefined behavior. The memory probably isn’t cleared.
It’s pure luck your print statement works. When an object goes out of scope or is deleted, the memory is marked as released and not actually erased. The program can reclaim it and overwrite it.