C++ malloc error
I'm a Java programmer, but now I have to write a little bit of code in c++. I learned the basics of C++ a couple of years ago, so I'm not really fit.
I wrote a little class which describes a Polynomial. Here it is:
#include "Polynom.h"
#include <iostream>
using namespace std;
Polynom::Polynom()
{
this->degree = 0;
this->coeff = new int[0];
}
Polynom::Polynom(int degree)
{
this->degree = degree;
this->coeff = new int[degree + 1];
}
Polynom::~Polynom()
{
delete coeff;
}
void Polynom::setDegree(int degree)
{
this->degree = degree;
}
void Polynom::setCoeffs(int* coeff)
{
this->coeff = &*coeff;
}
void Polynom::print()
{
int i;
for(i = degree; i >= 0; i --)
{
cout<<this->coeff[i];
if(i != 0)
cout<<"x^"<<i;
if(i > 0)
{
if(coeff开发者_开发百科[i - 1] < 0)
cout<<" - ";
else
cout<<" + ";
}
}
}
Okay, now I tried to read the degree and the coefficients of the polynomial and print it in the console. Here's the code for that:
#include <iostream>
#include "Polynom.h"
using namespace std;
int main()
{
int degree;
cout<<"degree = ";
cin>>degree;
int* coeff = new int[degree];
int i;
for(i = 0; i <= degree; i++)
{
cout<<"coeff[x^"<<i<<"] = ";
cin>>coeff[i];
}
Polynom *poly = new Polynom(degree);
//poly->setDegree(degree);
poly->setCoeffs(coeff);
cout<<"The input polynome is: ";
poly->print();
return 0;
}
When compiling the code, everything is ok. When running, if I give an even degree and then give some coefficients, the program runs normally. But: if I define an odd degree (for example 3 or 5) and then give the coefficients, the program does not print the polynome and returns the following error:
malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Why does this happen? Where didn't I allocate enough memory for the array? I googled for this error and stumbled upon this page, but the solution mentioned there did not help me much.
Maybe you can see another problem in my code? I would really appreciate your help.
Thanks in advance.
There is a vast amount wrong with your code. C++ is nothing like Java and it seems that you are using pointers as if they are like references in Java, which they emphatically are not.
Polynom::Polynom()
{
this->degree = 0;
this->coeff = new int[0];
}
This creates an array of size zero, which is legal in C++, but almost never what you want.
Polynom::~Polynom()
{
delete coeff;
}
Arrays in C++ must be deleted with delete[]:
Polynom::~Polynom()
{
delete [] coeff;
}
This:
void Polynom::setDegree(int degree)
{
this->degree = degree;
}
makes no sense - you change the degree, but not the array it is associated with.
void Polynom::setCoeffs(int* coeff)
{
this->coeff = &*coeff;
}
I have no idea what you think this is doing, and I suspect you don't either. And you have a memory leak.
That was just for starters - Isuspect there is much more bad stuff. You need to do two things:
Read a book on C++. As you have programming experience, I recommend Accelerated C++.
Forget about your Java knowledge. As I said, the two languages have almost nothing in common.
int* coeff = new int[degree];
int i;
for(i = 0; i <= degree; i++)
You are allocating space for degree
elements and putting there degree+1
elements... The behavior is undefined.
In your main()
function, int *coeff = new int[degree]
gives you a length-degree
array, with element indices ranging from 0
to degree-1
, inclusive. In your loop, you are accessing elements 0
to degree
, inclusive. This is undefined behaviour, which may or may not cause runtime errors, etc.
The statement i <= degree
in the for
loop is causing it. Since array indexes start from 0
the valid range is 0->degree-1
. Since you are writing to an invalid memory location your program is behaving unpredictably.
精彩评论