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?
}
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?
}
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