473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

conflict std::vector and delete operator?!

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;
}

Jul 22 '05 #1
2 1980
Stijn Oude Brunink wrote:
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
Jul 22 '05 #2

"Stijn Oude Brunink" <so**********@chello.nl> wrote in message
news:hj******************@amsnews03.chello.com...
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[i] = rhs.x[i]; }
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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: bartek d | last post by:
Hello, Regarding my previous question about a class which is used to store a variable type vector. I tried to be more elaborate on the code. I'd be grateful for your suggestions. Am I going in...
27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
0
by: Jason Heyes | last post by:
I wrote a previous post that asked whether there was a reference-counted implementation of std::vector. Apparantly there wasn't. So my next question is, is it possible to write your own shared...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
24
by: simon | last post by:
Hi, First some background. I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2;
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
13
by: linux_bp | last post by:
I have an stl vector array which stores the pointers to objects. To delete the array i am using: std::vector<*foo> bar; .... for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )...
7
by: imutate | last post by:
How do I implement << ala std::cout for vector template ? I already have the following: #include <vector> template < typename T > class Vec : public std::vector< T { public: Vec() { } Vec(...
6
by: lokchan | last post by:
i want to create a vector of pointer s.t. it can handle new and delete but also have std::vector interface can i implement by partial specialization and inherence like follow ? #include...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.