conflict std::vector and delete operator?!

S

Stijn Oude Brunink

Hello,

I want to use the vector class to work with arrays of classes but I seem to
get in conflict with the delete operator used in the specific class. The
code below gives an assertion?! How is that possible? I have to free the
allocated memory in my class CTest but combination with a vector it seems as
if this is not allowed.

(I'm using VC 6.0)
Can somebody give me a clue what going and how to tackle this problem?

regards

Stijn

#include <vector>
using namespace std;

class CTest
{
public:
CTest();
~CTest();

double* x;

};

CTest::CTest()
{
x=new double[10];
}

CTest::~CTest()
{
if(x!=NULL)
delete [] x;
x=NULL;
}


int main(int argc, char* argv[])
{
vector<CTest> arrTest;
arrTest.resize(1);

return 0;
}
 
V

Victor Bazarov

Stijn said:
I want to use the vector class to work with arrays of classes but I seem to
get in conflict with the delete operator used in the specific class. The
code below gives an assertion?! How is that possible? I have to free the
allocated memory in my class CTest but combination with a vector it seems as
if this is not allowed.

(I'm using VC 6.0)
Can somebody give me a clue what going and how to tackle this problem?

You failed to follow "the Rule of Three". Look for it on the Web.
Hint: define a proper copy-constructor.
regards

Stijn

#include <vector>
using namespace std;

class CTest
{
public:
CTest();
~CTest();

double* x;

};

CTest::CTest()
{
x=new double[10];
}

CTest::~CTest()
{
if(x!=NULL)
delete [] x;

There is no need to test 'x' for NULL. delete NULL does nothing.
x=NULL;
}


int main(int argc, char* argv[])
{
vector<CTest> arrTest;
arrTest.resize(1);

return 0;
}


Victor
 
J

John Harrison

Stijn Oude Brunink said:
Hello,

I want to use the vector class to work with arrays of classes but I seem to
get in conflict with the delete operator used in the specific class. The
code below gives an assertion?! How is that possible? I have to free the
allocated memory in my class CTest but combination with a vector it seems as
if this is not allowed.

(I'm using VC 6.0)
Can somebody give me a clue what going and how to tackle this problem?

regards

Stijn

#include <vector>
using namespace std;

class CTest
{
public:
CTest();
~CTest();

double* x;

};

CTest::CTest()
{
x=new double[10];
}

CTest::~CTest()
{
if(x!=NULL)
delete [] x;
x=NULL;
}


int main(int argc, char* argv[])
{
vector<CTest> arrTest;
arrTest.resize(1);

return 0;
}

This is nothing to do with vector. The following vector-less code would also
crash

int main(int argc, char* argv[])
{
CTest x;
CTest y(x);
}

The problem is that your CTest objects are not copyable. Here's a version of
CTest that is

class CTest
{
public:
CTest() { x = new double[10]; }
~CTest() { delete[] x; }
CTest(const CTest& rhs) { x = new double[10]; for (int i = 0; i < 10; ++i)
x = rhs.x; }
CTest& operator=(const CTest& rhs) { CTest tmp(rhs); swap(tmp); return
*this; }
void swap(CTest& rhs) { double* t = x; x = rhs.x; rhs.x = t; }
private:
double* x;
};

Untested code.

john
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top