B
Bryan
I hate it when people reinvent the wheel. This is some legacy code that
I believe may be causing some memory issues. I think there may be a
problem in the way that the CVector class is kill_resize'd or assigned.
Any thoughts?
class CVector
{
// types:
public:
typedef double data_type;
// Constructors
public:
CVector();
CVector(long source_length);
CVector(long source_length, const data_type* const source_data);
CVector(const CVector &fil);
virtual ~CVector();
// Operators
public:
CVector& operator= (const CVector& source);
// Functions
public:
data_type* begin() const { return data; };
data_type* end() const { return data + length; };
long size() const { return length; };
bool empty() const { return size() == 0; }
// set new vector length _without_ copying
void kill_resize (long source_length);
data_type* data;
// Member variables:
protected:
long length;
};
// Constructors:
CVector::CVector(): data(0), length(0) {}
CVector::CVector(long source_length):
length(source_length), data(new data_type[source_length]) {}
CVector::CVector(long source_length, const data_type* const source_data):
length(source_length), data(new data_type[source_length])
{
if (data) memcpy(data, source_data, length * sizeof(data_type));
}
CVector::CVector(const CVector &v):
length(v.length), data(new data_type[v.length])
{
if (data) memcpy(data, v.data, length * sizeof(data_type));
}
// Destructor
CVector::~CVector()
{
if (data) delete[] data;
data = 0; // do not free again.
length = 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
// Change the length of an existing vector WITHOUT COPYING
void CVector::kill_resize (long source_length)
{
if (data && (source_length != length))
{
delete[] data; // there was something in here
data = 0; // do not free again.
}
if (!data) data = new data_type[source_length]; // get memory
length = source_length;
}
/////////////////////////////////////////////////////////////////////////////////////////
CVector& CVector:perator= (const CVector &source)
{
if (source.data == NULL) {
if (data != NULL) {
delete[] data; // there was something in here
data = 0; // do not free again.
}
length = 0;
return *this;
}
if (data == source.data) {
return *this;
}
kill_resize (source.length);
if (source.data) memcpy(data, source.data, length * sizeof(data_type));
return *this;
}
I believe may be causing some memory issues. I think there may be a
problem in the way that the CVector class is kill_resize'd or assigned.
Any thoughts?
class CVector
{
// types:
public:
typedef double data_type;
// Constructors
public:
CVector();
CVector(long source_length);
CVector(long source_length, const data_type* const source_data);
CVector(const CVector &fil);
virtual ~CVector();
// Operators
public:
CVector& operator= (const CVector& source);
// Functions
public:
data_type* begin() const { return data; };
data_type* end() const { return data + length; };
long size() const { return length; };
bool empty() const { return size() == 0; }
// set new vector length _without_ copying
void kill_resize (long source_length);
data_type* data;
// Member variables:
protected:
long length;
};
// Constructors:
CVector::CVector(): data(0), length(0) {}
CVector::CVector(long source_length):
length(source_length), data(new data_type[source_length]) {}
CVector::CVector(long source_length, const data_type* const source_data):
length(source_length), data(new data_type[source_length])
{
if (data) memcpy(data, source_data, length * sizeof(data_type));
}
CVector::CVector(const CVector &v):
length(v.length), data(new data_type[v.length])
{
if (data) memcpy(data, v.data, length * sizeof(data_type));
}
// Destructor
CVector::~CVector()
{
if (data) delete[] data;
data = 0; // do not free again.
length = 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
// Change the length of an existing vector WITHOUT COPYING
void CVector::kill_resize (long source_length)
{
if (data && (source_length != length))
{
delete[] data; // there was something in here
data = 0; // do not free again.
}
if (!data) data = new data_type[source_length]; // get memory
length = source_length;
}
/////////////////////////////////////////////////////////////////////////////////////////
CVector& CVector:perator= (const CVector &source)
{
if (source.data == NULL) {
if (data != NULL) {
delete[] data; // there was something in here
data = 0; // do not free again.
}
length = 0;
return *this;
}
if (data == source.data) {
return *this;
}
kill_resize (source.length);
if (source.data) memcpy(data, source.data, length * sizeof(data_type));
return *this;
}