Skip to main content
2 of 2
Code formatting, pull the question out of comments

Memory Allocation in C++

Does the memory allocated by class pointers get freed by the class destructor once the class method/function goes out of scope? Or do I need to manually free the memory allocated by the pointer (C++). For example, here's a snippet of some simple code :

double * const  Area()
{
  double * Area = new double;
  *Area = itslength*itswidth;
  return Area;
  //itslength and itswidth are private class variables
}

So in this example, do I have to free the memory allocated by the Area pointer, or does the class destructor do this by default once the function returns/goes out of scope? If I have to manually free memory, can you modify my code to free the memory?