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

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++). forFor example, here's a snippet of some simple code :

    `doubledouble * const  Area()
    {
    double * Area = new double   ;double;
    *Area = itslength*itswidth ;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?*/`

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?

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?*/`

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?

Source Link

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?*/`