472,976 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

destructor trouble when using overloaded + operator

Hi,
I've ran into some trouble with an overloaded + operator, maybe
someone can give me some hints what to look out for.

I've got my own custom vector class, as a part of that I've overloaded
the + operator by means of a friend function. Everything worked fine
until I decided to use a variable array size (by using new/delete),
now I get an error when a temporary object is deleted (e.g. after
addition), the error occurs at the delete statement in the destructor,
I can see that the object isn't initialized... the error I get is
something like "debug assertion failed".

any things to look out for?
thanks...
I

Sep 16 '08 #1
9 1999
itdevries wrote:
Hi,
I've ran into some trouble with an overloaded + operator, maybe
someone can give me some hints what to look out for.

I've got my own custom vector class, as a part of that I've overloaded
the + operator by means of a friend function. Everything worked fine
until I decided to use a variable array size (by using new/delete),
now I get an error when a temporary object is deleted (e.g. after
addition), the error occurs at the delete statement in the destructor,
I can see that the object isn't initialized... the error I get is
something like "debug assertion failed".

any things to look out for?
a) Post code. As of now, we do not even know whether + is used to denote
concatenation or member-wise addition (just to name the most common).

b) A delete statement in the destructor is weird. The destructor should be
empty (as far as I can see; but then again: post code). Similarly, variable
array sizes with a vector class are handled not via new/delete, but by the
vector itself. See std::vector for an example.
Best

Kai-Uwe Bux
Sep 16 '08 #2
itdevries wrote:
Hi,
I've ran into some trouble with an overloaded + operator, maybe
someone can give me some hints what to look out for.

I've got my own custom vector class, as a part of that I've overloaded
the + operator by means of a friend function. Everything worked fine
until I decided to use a variable array size (by using new/delete),
now I get an error when a temporary object is deleted (e.g. after
addition), the error occurs at the delete statement in the destructor,
I can see that the object isn't initialized... the error I get is
something like "debug assertion failed".
If you want constructive help, post the shortest compilable code that
shows your problem.

--
Ian Collins.
Sep 16 '08 #3
On 16 sep, 10:20, Kai-Uwe Bux <jkherci...@gmx.netwrote:
itdevries wrote:
Hi,
I've ran into some trouble with an overloaded + operator, maybe
someone can give me some hints what to look out for.
I've got my own custom vector class, as a part of that I've overloaded
the + operator by means of a friend function. Everything worked fine
until I decided to use a variable array size (by using new/delete),
now I get an error when a temporary object is deleted (e.g. after
addition), the error occurs at the delete statement in the destructor,
I can see that the object isn't initialized... the error I get is
something like "debug assertion failed".
any things to look out for?

a) Post code. As of now, we do not even know whether + is used to denote
concatenation or member-wise addition (just to name the most common).

b) A delete statement in the destructor is weird. The destructor should be
empty (as far as I can see; but then again: post code). Similarly, variable
array sizes with a vector class are handled not via new/delete, but by the
vector itself. See std::vector for an example.

Best

Kai-Uwe Bux
ok, I'll try to keep it shor:

// header:
friend vec3 operator+(const vec3& vecA, const vec3& vecB);
private:
float* m_coord;

//______________________________________
// constructor:
vec3::vec3(const int& len){
m_len = len;
m_coord = new float[m_len];
}
//______________________________________
// destructor:
vec3::~vec3(void){
delete [] this->m_coord;
}

// error at this statement:
Rd0 = Rd0 + Atotal * vec;

// Rd0, are vectors of the vec3 class, Atotal is a matrix (I've also
defined multiplication of matrix x vector in another class)

thanks, IdV


Sep 16 '08 #4
itdevries wrote:
On 16 sep, 10:20, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>itdevries wrote:
Hi,
I've ran into some trouble with an overloaded + operator, maybe
someone can give me some hints what to look out for.
I've got my own custom vector class, as a part of that I've overloaded
the + operator by means of a friend function. Everything worked fine
until I decided to use a variable array size (by using new/delete),
now I get an error when a temporary object is deleted (e.g. after
addition), the error occurs at the delete statement in the destructor,
I can see that the object isn't initialized... the error I get is
something like "debug assertion failed".
any things to look out for?

a) Post code. As of now, we do not even know whether + is used to denote
concatenation or member-wise addition (just to name the most common).

b) A delete statement in the destructor is weird. The destructor should
be empty (as far as I can see; but then again: post code). Similarly,
variable array sizes with a vector class are handled not via new/delete,
but by the vector itself. See std::vector for an example.

Best

Kai-Uwe Bux

ok, I'll try to keep it shor:
Informative is better than short. The FAQ advises to post a minimal but
compilable example of code that exhibits the problem.

// header:
friend vec3 operator+(const vec3& vecA, const vec3& vecB);
private:
float* m_coord;

//______________________________________
// constructor:
vec3::vec3(const int& len){
m_len = len;
m_coord = new float[m_len];
}
//______________________________________
// destructor:
vec3::~vec3(void){
delete [] this->m_coord;
}

// error at this statement:
Rd0 = Rd0 + Atotal * vec;

// Rd0, are vectors of the vec3 class, Atotal is a matrix (I've also
defined multiplication of matrix x vector in another class)
One thing that comes to mind is the assignment operator. Since you have a
pointer member, copy-constructor, assignment, and destructor need some
care. That is one thing to watch out for since you have an error on
something that is related to self-assignment.

The operator+ looks as follows, I presume:

vec3 operator+ (const vec3& vecA, const vec3& vecB) {
assert( vecA.m_len == vecB.m_len );
vec3 result ( vecA );
for ( vec3::size_type i = 0; i < vecA.m_len; ++i ) {
result.m_coord += vecB.m_coord[i];
}
return ( result );
}

So, if you have vectors of different dimensions, addition would be
ill-defined. I mention that because you suggested that you ran into
troubles when you started with variable sizes.
Best

Kai-Uwe Bux
Sep 16 '08 #5
itdevries wrote:
On 16 sep, 10:20, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>itdevries wrote:
>>Hi,
I've ran into some trouble with an overloaded + operator, maybe
someone can give me some hints what to look out for.
I've got my own custom vector class, as a part of that I've overloaded
the + operator by means of a friend function. Everything worked fine
until I decided to use a variable array size (by using new/delete),
now I get an error when a temporary object is deleted (e.g. after
addition), the error occurs at the delete statement in the destructor,
I can see that the object isn't initialized... the error I get is
something like "debug assertion failed".
any things to look out for?
a) Post code. As of now, we do not even know whether + is used to denote
concatenation or member-wise addition (just to name the most common).

b) A delete statement in the destructor is weird. The destructor should be
empty (as far as I can see; but then again: post code). Similarly, variable
array sizes with a vector class are handled not via new/delete, but by the
vector itself. See std::vector for an example.

Best

Kai-Uwe Bux

ok, I'll try to keep it shor:

// header:
friend vec3 operator+(const vec3& vecA, const vec3& vecB);
private:
float* m_coord;

//______________________________________
// constructor:
vec3::vec3(const int& len){
m_len = len;
m_coord = new float[m_len];
}
//______________________________________
// destructor:
vec3::~vec3(void){
delete [] this->m_coord;
}

// error at this statement:
Rd0 = Rd0 + Atotal * vec;

// Rd0, are vectors of the vec3 class, Atotal is a matrix (I've also
defined multiplication of matrix x vector in another class)

thanks, IdV

It is hard to say for certain since you have not posted compilable code,
but I would guess that you are having trouble stemming from the fact
that you have not defined a copy constructor or assignment operator.

When the temporary vec3 is copied to Rd0, it's m_coord pointer is
copied. Then the temporary is destructed leaving Rd0 with an invalid
pointer. Then Rd0 is eventually destructed and attempts to delete an
invalid pointer. This is all conjecture of course.

In general, if you design a class that needs a destructor, then it will
almost always need a copy constructor and assignment operator as well,
exactly because of the problem you seem to be encountering.

The easiest way to fix your program would be make m_coord a
std::vector<floatinstead of using new/delete to manage memory
yourself. std::vector already correctly implements copy construction
and assignment.

Finally, a code example that may more clearly demonstratethe problem
that I think you are seeing. How many times is new called? How many
times is delete called?

#include <iostream>

class bad
{
public:
bad()
{
m_p = new int[5];
std::cout << "new: " << m_p << std::endl;
}

~bad()
{
std::cout << "delete: " << m_p << std::endl;
delete [] m_p;
}
private:
int * m_p;
};

int main()
{
bad b1;
bad b2(b1);
return 0;
}

--
Alan Johnson
Sep 16 '08 #6
On 16 sep, 10:55, Alan Johnson <aw...@yahoo.comwrote:
itdevries wrote:
On 16 sep, 10:20, Kai-Uwe Bux <jkherci...@gmx.netwrote:
itdevries wrote:
Hi,
I've ran into some trouble with an overloaded + operator, maybe
someone can give me some hints what to look out for.
I've got my own custom vector class, as a part of that I've overloaded
the + operator by means of a friend function. Everything worked fine
until I decided to use a variable array size (by using new/delete),
now I get an error when a temporary object is deleted (e.g. after
addition), the error occurs at the delete statement in the destructor,
I can see that the object isn't initialized... the error I get is
something like "debug assertion failed".
any things to look out for?
a) Post code. As of now, we do not even know whether + is used to denote
concatenation or member-wise addition (just to name the most common).
b) A delete statement in the destructor is weird. The destructor should be
empty (as far as I can see; but then again: post code). Similarly, variable
array sizes with a vector class are handled not via new/delete, but bythe
vector itself. See std::vector for an example.
Best
Kai-Uwe Bux
ok, I'll try to keep it shor:
// header:
friend vec3 operator+(const vec3& vecA, const vec3& vecB);
private:
float* m_coord;
//______________________________________
// constructor:
vec3::vec3(const int& len){
m_len * * * * * * *= len;
m_coord * * * * * *= new float[m_len];
}
//______________________________________
// destructor:
vec3::~vec3(void){
delete [] this->m_coord;
}
// error at this statement:
Rd0 * * * * * * * *= Rd0 + Atotal * vec;
// Rd0, are vectors of the vec3 class, Atotal is a matrix (I've also
defined multiplication of matrix x vector in another class)
thanks, IdV

It is hard to say for certain since you have not posted compilable code,
but I would guess that you are having trouble stemming from the fact
that you have not defined a copy constructor or assignment operator.

When the temporary vec3 is copied to Rd0, it's m_coord pointer is
copied. *Then the temporary is destructed leaving Rd0 with an invalid
pointer. *Then Rd0 is eventually destructed and attempts to delete an
invalid pointer. *This is all conjecture of course.

In general, if you design a class that needs a destructor, then it will
almost always need a copy constructor and assignment operator as well,
exactly because of the problem you seem to be encountering.

The easiest way to fix your program would be make m_coord a
std::vector<floatinstead of using new/delete to manage memory
yourself. *std::vector already correctly implements copy construction
and assignment.

Finally, a code example that may more clearly demonstratethe problem
that I think you are seeing. *How many times is new called? *How many
times is delete called?

#include <iostream>

class bad
{
public:
* * *bad()
* * *{
* * * * *m_p = new int[5];
* * * * *std::cout << "new: * *" << m_p << std::endl;
* * *}

* * *~bad()
* * *{
* * * * *std::cout << "delete: " << m_p << std::endl;
* * * * *delete [] m_p;
* * *}
private:
* * *int * m_p;

};

int main()
{
* * *bad b1;
* * *bad b2(b1);
* * *return 0;

}

--
Alan Johnson- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -
Alan, thanks very much, you are right, that was the error. I'm aware
of the standard vector class but I've got some reasons for not using
it.
thanks very much!
Igor
Sep 16 '08 #7
On 9ÔÂ16ÈÕ, ÏÂÎç4ʱ31·Ö, itdevries <itdevr...@gmail..comwrote:
On 16 sep, 10:20, Kai-Uwe Bux <jkherci...@gmx.netwrote:


itdevries wrote:
Hi,
I've ran into some trouble with an overloaded + operator, maybe
someone can give me some hints what to look out for.
I've got my own custom vector class, as a part of that I've overloaded
the + operator by means of a friend function. Everything worked fine
until I decided to use a variable array size (by using new/delete),
now I get an error when a temporary object is deleted (e.g. after
addition), the error occurs at the delete statement in the destructor,
I can see that the object isn't initialized... the error I get is
something like "debug assertion failed".
any things to look out for?
a) Post code. As of now, we do not even know whether + is used to denote
concatenation or member-wise addition (just to name the most common).
b) A delete statement in the destructor is weird. The destructor shouldbe
empty (as far as I can see; but then again: post code). Similarly, variable
array sizes with a vector class are handled not via new/delete, but by the
vector itself. See std::vector for an example.
Best
Kai-Uwe Bux

ok, I'll try to keep it shor:

// header:
friend vec3 operator+(const vec3& vecA, const vec3& vecB);
private:
float* m_coord;

//______________________________________
// constructor:
vec3::vec3(const int& len){
m_len = len;
m_coord = new float[m_len];

}

//______________________________________
// destructor:
vec3::~vec3(void){
delete [] this->m_coord;

}

// error at this statement:
Rd0 = Rd0 + Atotal * vec;

// Rd0, are vectors of the vec3 class, Atotal is a matrix (I've also
defined multiplication of matrix x vector in another class)

thanks, IdV- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -
it seems that the problem was related to shallow copy or array
overflow.
Please refine the copy ctor and assignment operator
Sep 16 '08 #8
itdevries wrote:
[....]
Your "example" doesn't have main(), classes declaration and
implementation, therefore it is not compilable example.
Sep 16 '08 #9
I shoud qout:
****
On Sep 16, 11:45*am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
One thing that comes to mind is the assignment operator. Since you have a
pointer member, copy-constructor, assignment, and destructor need some
care.
****

regards,
FM.
Sep 16 '08 #10

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

Similar topics

3
by: Scott Brady Drummonds | last post by:
Hi, all, I've a fairly small piece of code that is causing me problems. I'm using std::string and am building a string of several dozen characters using several of std::string's functions: a...
3
by: ganesh.tambat | last post by:
Hi, Please see below a piece of code. Here I am trying to create a linked list by attaching two linked list together. I have overloaded operator + for this. Now the output always says that the...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
5
by: junw2000 | last post by:
I use the code below to study delete and destructor. #include <iostream> using namespace std; struct A { virtual ~A() { cout << "~A()" << endl; }; //LINE1 void operator delete(void* p) {...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
10
by: piboye | last post by:
Hi ! I'm a academician in china. I have been intereted in C++ lasting. In reading the C++ Primer book, i have a trouble about union. In the book ,it said that union can have constructors and...
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
11
by: blackx | last post by:
I overloaded the * operator function as follows: Polynomial Polynomial::operator*(const Polynomial &p) { Polynomial newPoly; Term * temp1 = this->head; Term * temp2 = p.head; double c =...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.