1

If I create an object without using the "new" keyword, how should I free its memory?

Example:

#include "PixelPlane.h"

int main(void)
{
     PixelPlane pixel_plane(960, 540, "TITLE");
     //How should the memory of this object be freed?
}


SuperSim135
  • 135
  • 1
  • 10
  • 3
    It happens automatically when the life-time of the variable `pixel_plane` ends, which happens when the scope ends. That leads to the objects destruction and the memory being released (in an implementation-defined manner). – Some programmer dude Nov 27 '19 at 18:14
  • If the object has a proper destructor, you don't need to. See: https://stackoverflow.com/questions/4036394/how-do-i-call-the-classs-destructor – Cristopher Rosales Nov 27 '19 at 18:14
  • Possible duplicate of [Calling delete on variable allocated on the stack](https://stackoverflow.com/questions/441831/calling-delete-on-variable-allocated-on-the-stack) – François Andrieux Nov 27 '19 at 18:15
  • 4
    Why the vote down? The question is properly presented. You should not vote people down just because you know the answer, they do not, and you think it is elementary. **Every** student of C++ needs to learn the answer to this, so it is useful information for many people. – Eric Postpischil Nov 27 '19 at 18:15
  • Is the memory automatically freed if `pixel_plane` is defined globally? – SuperSim135 Nov 27 '19 at 18:18
  • 1
    @SuperSim135 Yes, when the process terminates. – WhozCraig Nov 27 '19 at 18:20
  • @SuperSim135: That's a different question. – Nicol Bolas Nov 27 '19 at 18:30
  • 1
    Handy reading: [Storage class specifiers](https://en.cppreference.com/w/cpp/language/storage_duration) All of this is informative, but the section on Storage Duration is most relevant. – user4581301 Nov 27 '19 at 18:37

1 Answers1

3

pixel_plane is a variable with automatic storage duration (i.e. a normal local variable).

It will be freed when execution of the enclosing scope ends (i.e. when the function returns).


This is an example of a local variable that doesn't have automatic storage duration.

void my_function()
{
    static PixelPlane pixel_plane(960, 540, "TITLE");
    // pixel_plane has static storage duration - it is not freed until the program exits.
    // Also, it's only allocated once.
}

This is an example of an enclosing scope that is not a function:

int main(void)
{
    PixelPlane outer_pixel_plane(960, 540, "TITLE");

    {
        PixelPlane inner_pixel_plane(960, 540, "TITLE");
    } // inner_pixel_plane freed here

    // other code could go here before the end of the function

} // outer_pixel_plane freed here
user253751
  • 57,427
  • 7
  • 48
  • 90
  • 1
    Storage is freed when execution of the associated block ends, not when scope ends. Scope does not have any end in time, just in place. Scope is **where** in source code identifiers are visible. Storage duration or lifetime is **when** during program execution objects exist. An automatic object ceases to exist when execution of its associated block ends (and not merely when program control temporarily leaves the block, as when a subroutine is called). – Eric Postpischil Nov 27 '19 at 18:32