ImaginaryHuman072889
ImaginaryHuman072889

Reputation: 5185

C++ accessing object on heap after end of pointer scope

I've been reading various questions and answers about pointers in C++ on SO but I still am fundamentally not understanding something.

I've read that pointers allow for dynamic memory allocation, by not actually deleting an object until explicitly told to do so. This I understand, but what I don't understand is how an object on the heap is ever referred to again after the scope of its pointer ends.

See code below.

{
   myClass* x = new myClass();
}
// How to access the object that x points to here?

I understand that at the end of the scope, x (the pointer itself) no longer exists, but the object (that the pointer was pointing to) still does exist on the heap.

My question is: How is that address in memory ever accessed again if the pointer that points to that address in memory no longer exists?

Upvotes: 2

Views: 1648

Answers (2)

Chiel
Chiel

Reputation: 6194

Here you can find a small example that shows the differences between (1) allocation without proper deletion, (2) allocation with proper deletion, and (3) allocation using a smart pointer that deletes itself when it goes out of scope. Just run the program to see that the first example leaks memory.

#include <iostream>
#include <memory>

class A
{
    public:
        A(std::string name) : name_(name) 
        {
            std::cout << "My name is " << name_ << ", and I make mess...\n";
        }
        ~A()
        {
            std::cout << "My name is " << name_ << ", and I clean up...\n";
        }
    private:
        std::string name_;
};

int main()
{
    // 1. Leaks.
    A* a0;
    a0 = new A("a0");

    // 2. Does not leak.
    A* a1;
    a1 = new A("a1");
    delete a1;

    // 3. Does not leak.
    std::unique_ptr<A> a2 = std::make_unique<A>("a2");

    return 0;
}

Upvotes: 0

cdhowie
cdhowie

Reputation: 169008

How is that address in memory ever accessed again if the pointer that points to that address in memory no longer exists?

If the pointer value is not stored elsewhere, the object is "leaked" and becomes inaccessible. The object will continue to exist until the process terminates, at which point the operating system will reclaim the memory used by the object.

what I don't understand is how an object on the heap is ever referred to again after the scope of its pointer ends.

The pointer would have to be copied somewhere else. For example, you might put the pointer into a vector or a map before the local pointer goes out of scope, and then the object can still be accessed through the pointer stored in the vector/map:

int main() {
    std::vector<myClass *> objects;

    {
        myClass *x = new myClass();
        objects.push_back(x);
    }

    // The heap-allocated object can be accessed through objects[0] now
    objects[0]->doSomething();

    // Don't forget to clean up (or use smart pointers)
    for (auto i : objects) { delete i; }

    return 0;
}

Note that it's useful to learn how to use raw pointers when learning C++, but at some point you should switch to smart pointers like std::unique_ptr, which will automatically delete the pointed-to object for you:

{
    std::unique_ptr<myClass> x{new myClass()};
}
// The myClass object has already been deleted by the smart pointer

Upvotes: 7

Related Questions