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 9 1833
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
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.
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
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
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
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
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
itdevries wrote:
[....]
Your "example" doesn't have main(), classes declaration and
implementation, therefore it is not compilable example.
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. This discussion thread is closed Replies have been disabled for this discussion. Similar topics
3 posts
views
Thread by Scott Brady Drummonds |
last post: by
|
3 posts
views
Thread by ganesh.tambat |
last post: by
|
11 posts
views
Thread by Ken Durden |
last post: by
|
5 posts
views
Thread by junw2000 |
last post: by
|
23 posts
views
Thread by Ben Voigt |
last post: by
|
10 posts
views
Thread by piboye |
last post: by
|
9 posts
views
Thread by rohits123 |
last post: by
| |
12 posts
views
Thread by Rahul |
last post: by
| | | | | | | | | | |