Hello! I have a project for my university, and I am having problem:
1 2 3 4 5 6 7
"Windows has triggered a breakpoint in polynome v0.0.0.0.exe.
This may be due to a corruption of the heap, which indicates a bug in polynome v0.0.0.0.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while polynome v0.0.0.0.exe has focus.
The output window may have more diagnostic information."
after debugging step by step I have found that the error relies here:
1 2 3 4 5 6
polynome::~polynome()
{ //p's length is 11 here
//when p was of length 1 this code worked without error
cout<<"~: "<<*this<<endl; //I have created this line to test if *this is as expected: it is
delete []p; //here is the error
}
this is the definition of class polynome:
1 2 3 4 5 6
class polynome
{
double *p;
unsigned d;
... //declaration of methods
};
this is the method calling the destructor (the one with the problem)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
polynome polynome::operator+=(constchar* const s)
{polynome p2(s);
if(d>=p2.d)
for(int i=0;i<=p2.d;i++)
p[i]+=p2.p[i];
if(d<p2.d)
{polynome p3(*this);
operator=(p2);
for(int i=0;i<=p3.d;i++)
p[i]+=p3.p[i];
}
//from the debugging all is as expected here
return *this; //after here the first destructor cause the problem
}
Merry Christmas to you all.
if you want to see any other method I'll provide it
no way to debug without the full code, but I will check these things
- all for loops, i=0; i<= ....
are u sure it is <= or < -- double check?
- in the for loops using int i --- should they be "unsigned int i"
- some times the values can be such that int can overflow, vs unsigned int..
@LowestOne
Thank you the answer was that I used d instead of d+1
but for the idea that operator+= should return a reference I don't think so (please correct me if I was wrong and tell me why)
@ashishkumar
-no it is <= because d is the degree of the polynomial and I should see the coefficient of the monomial of the degree d
-yes probably but I already corrected it by putting:i<=int(d). (I did it after posting it)
-Probably to correct this I should make it like this:
1 2 3 4 5 6
class polynome
{
double *p;
int d;
... //declaration of methods
};
because sometimes (in the operator<< for cout), I must use int because it is a reverse for loop and it stops when i becomes negative
anyway thanks for helping me LowestOne nailed the problem
I'm sorry in case my English wasn't correct.
Both += and = operators return references. This is the return from the assignment, it's not obvious. Consider istream::get(), which returns a character.
Why should we copy ch when we assign it the result of get()?
The + operator does return a new object because a = b + c can not modify b or c. With the addition, we create a temporary object, and then assign it to a. a = a + b is doing more than it needs to because that temp is still created.
#include <iostream>
void print( constchar* str ){ std::cout << str << std::endl; }
struct Number
{
int value;
Number( void ) :value(0) { print("Default Ctor" );}
Number( constint v ) :value(v) { print("Assignment Ctor");}
Number( const Number& other ):value(other.value){ print("Copy Ctor" );}
Number& operator=( const Number& other )
{
print( "Assignment" );
this->value = other.value;
return *this;
}
Number& operator+=( const Number& other )
{
print( "Compound assignment" );
this->value += other.value;
return *this;
}
Number operator+( const Number& other )
{
print( "Addition" );
return Number( this->value + other.value );
// also calls assignment ctor
// return this->value + other.value;
}
};
int main( void )
{
Number a(1); // -> Assignment Ctor
Number b = 2; // -> Assignment Ctor
a = a + b; // -> Addition, Assignment Ctor, Assignment
a += b; // -> Compound Assignment
return 0;
}
This is the same reason it is better to do ++i than i++. The difference is that ++i can return a reference to i:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Number& operator++( void )
{
++this->value;
return *this;
}
Number operator++( int ) // nevermind the int, just makes it a postfix operator
{
Number temp( *this );
++this->value;
return temp;
}
// ...
Number a(1);
if ( a++ == 1 ) // increment needs to be visible after the comparison
if ( ++a == 3 ) // increment is visible before comparison