Free up memory in C++

Asked

Viewed 2,424 times

6

I am obliged to release memory of every variable that I will no longer be using?

If yes, how to do?

1 answer

7


No. You don’t have to release memory of variables. Never, at least of local variables (not variables that are part of another object. You have to free up memory of objects that are pointed by variables. There is an important conceptual difference there.

So variables by value never need to release their objects because the values are stored in the variable area itself. These variables may be in the stack or within objects.

Some variables are by reference. That is, they keep a pointer to objects, in general, in a dynamic area of flame memory heap.

Note that even if a variable by value is inside an object that is in the heap, its destruction will be automatic in the destruction of the object. The basic rule is that it is only necessary to have a manual or automatic release when there is the presence of pointer (it has several types), and according to its lifespan.

RAII and Smart Pointer

Many of these objects manage their lifespan and release the memory allocated to them when it is no longer necessary to use them. That is, when the pointing variable ceases to exist, the object itself takes care of destroying itself. So you don’t have to worry.

When the object does not have this feature, it is still possible to automate this with the so-called smart pointers. This makes you not have to worry about liberation.

These techniques are always recommended.

Today a modern C++ program hardly has to worry about manual memory release.

Of course, if you are going to build a new class, you will probably have to create its self-management engine. This is one of the few moments where you need to deal with memory liberation. Even in these cases, most of the time it is possible to replace a raw pointer by a smart pointer (mentioned above) and not to worry either.

Handbook:

class Exemplo {
    resource* recurso;
    Exemplo () : recurso(new resource) { }
    ~Exemplo () { delete recurso; }
};

Automatic:

class Exemplo {
    std::unique_ptr<resource> recurso;
    Exemplo() : recurso(new resource) { }
};

Manual-making

Other than this, you can make use of manual release for learning purposes.

If you allocate the memory manually you will have to release symmetrically with the allocation. If you use the C technique using malloc(), will have to use a simple free(). This form is considered obsolete in most situations in C++.

If you use the operator new of C++, you will need to use delete to free the memory. If it is a array, will use new[] and delete[]. But again, I only advise the use of one of these memory controls within classes. The release, in general, will take place within the destructor.

Obviously in simple applications that will end soon or in some allocation that should last the entire execution of the application there are no leakage problems. The end of the application will free all memory allocated by it. The problem only occurs when there are more complex allocations that need to be dropped during execution to not jam memory.

  • It would be nice to add the case where "resource" is a simple member of the class (Resource resource), even if the object is in heap members that are not pointers are also automatically released when the object is destroyed. Pointers and references, even automatic, should be used only when there is a good reason.

  • 1

    @epx true, I was really looking forward to giving a good, improved response as a whole, I think I have an opportunity to talk about several things there.

  • Another pinch not to mention I didn’t mention RAII

  • @Anthonyaccioly I’m seeing some more changes. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.