Why the value of the local variable (its address is stored in the global variable) is released when the function has finished executing


This is my code

using namespace std;
int *ptr;
void hold(){
  int a=12;

int main(){
  cout<<"value of a="<<*ptr;

and I get value of a=12 This must not happen because compiler has freed the value at address of a so why 12 comes?

Freeing doesn't mean that the system will modify the value stored there (which would involve unnecessary overhead). It just means that this memory space is made available for future storage.