In these lines of C++ code:
int * p = new int(33);
delete(p);
*p = 13;
cout << *p << endl;
The output is 13;
P points to an address on the heap initially, then I use the delete keyword to deallocate p’s assigned memory address, but can still assign the memory address a value of 23; Is this the same address on the heap that p pointed to after “int * p = new int(33)” or does p point to an address on the stack after using delete(p)?
Deleting
psignals to whoever manages the memory (the OS) that the underlying space is now free to be re-allocated by someone else for their own use.p, however, still points to the same memory location and can be dereferenced to obtain the value of what’s in that memory — note that since that memory may now be used by someone else, the underlying bits might be different from what they were before.