By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,117 Members | 2,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,117 IT Pros & Developers. It's quick & easy.

destructor trouble when using overloaded + operator

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
itdevries wrote:
[....]
Your "example" doesn't have main(), classes declaration and
implementation, therefore it is not compilable example.
Sep 16 '08 #9

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.