Where to free memory ?

P

Phil

Hello all,
I kindly request your help for the following problem.
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);
// Do some processing here with classA
delete classA;
}

/////////////////////////////////////

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
 
M

mzdude

Hello all,
I kindly request your help for the following problem.
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));

}
Assume this is:
void ClassA::ClassA(int nItems)
: myTable( (double *)calloc(..) )
{
if( myTable == NULL )
throw std::bad_alloc();
}

if you were using std::vector

void ClassA::ClassA(int nItems)
: myTable(nItems,0.0)
{}
~ClassA()
{
// Should I free the memory allocated for myTable here?
// free(myTable);
// myTable=NULL;

}
Yes you should free memory here. The myTable=NULL can
be omitted.

If you are using std::vector, you don't even need the dtor.
/////////////////////////////////////

// ClassB.h //////////////////////////
#include "ClassA.h"
class ClassB
{
public:
  void ClassB();
protected:
  ClassA classA(int nItems);
This is invalid. Should just be ClassA classA;
Or given the code below
ClassA *classA;
/////////////////////////////////////

// ClassB.cpp ///////////////////////
void ClassB()
{
classA=new ClassA(10);
// Do some processing here with classA
delete classA;

}
classA is not declared as a pointer in your example.
This should refuse to compile.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top