0

I am currently learning about dynamically allocated memory and have written a short program for practise.

Here is the main function:

int main() {
    // Local variables are stored in the stack.
    // The heap is a pool of unused memory that can be used when the program dynamically allocates memory.
    // The `new` operator allocates memory within the heap and returns the address of that variable:
    int *p = new int;
    *p = 5;

    cout << "A variable has just been allocated in the heap" << endl;
    cout << "Its memory address is: " << p << endl;
    cout << "Its value is: " << *p << endl;

    // For local variables on the stack, memory management is performed automatically.
    // But on the heap, it must be done manually.
    // Memory must be freed when no longer needed:
    delete p; // This frees up the memory pointed to by p.

    // The `delete` keyword frees up the memory allocated for the variable, but does not delete the pointer p, because p is stored on the stack.
    // p is now a dangling pointer, it points to a non-existent memory location.

    // To test that it worked:
    cout << "The memory previously pointed to has now been deleted." << endl;
    cout << "Its memory address is: " << p << endl;
    cout << "Its value is: " << *p << endl;
}

So far it makes sense, except for line delete p. At that point, the memory is supposed to be 'freed up'. I read the following page for information:

Delete in c++ program is not freeing up memory

The answer said:

Calling delete whatever marks that memory as deallocated and free for future use.

However, when I run the program, the two sets of cout statements output the same thing, which to me suggests that the memory has not been freed up, as p still points to the same location, which has the same value.

After some tinkering, I found that I can reallocate p whether or not I ran delete p and I could change the memory address and its value in either case.

So what exactly does delete p do, I don't notice it making any difference to the program?

AkThao
  • 582
  • 4
  • 7
  • 23
  • 3
    Accessing `delete`d memory is Undefined behaviour. It can do literally anything, including what you see here. – Yksisarvinen Aug 24 '19 at 14:51
  • 4
    "When I drive a car without having a license, I never got stopped by a policeman. So what's the point of getting a driver's license?" – PaulMcKenzie Aug 24 '19 at 14:55
  • I see now. I've just read https://stackoverflow.com/questions/57143681/why-can-freed-dynamically-allocated-memory-still-be-accessed-after-a-delete-oper and I think I get it. Tell me if this analogy is correct: the memory location is a box and the value is an item stored in the box, whoever created the box owns it. When `delete` is used, the owner gives up ownership of the box and doesn't care what happens afterwards but they don't necessarily empty the box, which is why it could remain in the same state it was in when it was under the first owner's ownership. – AkThao Aug 24 '19 at 14:59
  • @AkThao If you want to introduce ownership have a look at [smart pointers](https://en.cppreference.com/w/cpp/memory). – πάντα ῥεῖ Aug 24 '19 at 15:07
  • @πάνταῥεῖ Thank you for the link, I didn't even know smart pointers exist. But in the context of normal pointers, is my analogy along the right lines? – AkThao Aug 24 '19 at 15:10

0 Answers0