Where to free memory?

Discussion in 'C++' started by Phil, Nov 9, 2009.

  1. Phil

    Phil Guest

    Hello all,
    Let's say I have two classes A and B defined as follows:

    // ClassA.h //////////////////////////
    class ClassA
    {
    public:
    void ClassA(int nItems); // constructor
    virtual ~ClassA();
    protected:
    double *myTable; // My table of nItems double values
    }
    /////////////////////////////////////


    // ClassA.cpp ///////////////////////
    void ClassA(int nItems)
    {
    // Allocate memory for the table of double values
    myTable=(double*)calloc(nItems, sizeof(double));
    }

    ~ClassA()
    {
    // Should I free the memory allocated for myTable here?
    // free(myTable);
    // myTable=NULL;
    }
    /////////////////////////////////////


    // ClassB.h //////////////////////////
    #include "ClassA.h"
    class ClassB
    {
    public:
    void ClassB();
    protected:
    ClassA classA(int nItems);
    }
    /////////////////////////////////////


    // ClassB.cpp ///////////////////////
    void ClassB()
    {
    classA=new ClassA(10);
    }
    /////////////////////////////////////

    My question is: when should I free the memory allocated for myTable
    array of object classA ?
    I am using MS Visual C++ 6.0 and Vista. Given the above code, I get a
    memory leak. OK.
    But if I free the memory as commented out in the destructor, I get an
    error message pointing at the free(myTable) line
    "memory check error at 0x02FDAA50 = 0xFE, should be 0xFD."
    What is the correct way to free the memory ?
    TIA,
    Phil
    Phil, Nov 9, 2009
    #1
    1. Advertising

  2. Phil wrote:
    > Let's say I have two classes A and B defined as follows:
    > [..shudder..]
    >
    > My question is: when should I free the memory allocated for myTable
    > array of object classA ?


    Please search the Web for, and read about, the "Rule of Three". Once
    you comprehend what it is saying, drop dynamic memory management and
    start using the standard containers like 'std::vector<double>'. You
    will not have to "free memory" ever again.

    > I am using MS Visual C++ 6.0 and Vista.


    Get the latest free MS VC++ Express Edition and enjoy a much improved
    compiler, a vastly better standard library, et cetera. Did I mention
    it's free?

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 9, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sam
    Replies:
    2
    Views:
    2,238
    Dhruv
    Jun 27, 2003
  2. Replies:
    1
    Views:
    805
    Sebastian Millies
    Nov 2, 2006
  3. Panduranga Chary

    How memory function free() knows how much memory to free.

    Panduranga Chary, Dec 27, 2007, in forum: C Programming
    Replies:
    2
    Views:
    413
    Keith Thompson
    Dec 27, 2007
  4. george
    Replies:
    0
    Views:
    1,107
    george
    Aug 29, 2008
  5. mohammed_a_o
    Replies:
    0
    Views:
    269
    mohammed_a_o
    Nov 30, 2010
Loading...

Share This Page