C++内存分配错误
我是一名 Java 程序员,但现在我必须用 C++ 编写一些代码。几年前我学习了 C++ 基础知识,所以我不太适合。
我写了一个描述多项式的小类。它是这样的:
#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<<" + ";
}
}
}
好的,现在我尝试读取多项式的次数和系数并将其打印在控制台中。这是代码:
#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;
}
编译代码时,一切正常。运行时,如果我给出一个偶数度数,然后给出一些系数,程序就可以正常运行。 但是:如果我定义一个奇数度数(例如 3 或 5),然后给出系数,程序不会打印多项式并返回以下错误:
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.
为什么这会发生吗?我哪里没有为数组分配足够的内存?我在 Google 上搜索了此错误,并偶然发现了此页面 ,但是那里提到的解决方案对我没有多大帮助。
也许您可以在我的代码中看到另一个问题?我非常感谢你的帮助。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码存在大量错误。 C++ 与 Java 完全不同,看起来您使用指针就好像它们就像 Java 中的引用一样,但它们显然不是。
这会创建一个大小为零的数组,这在 C++ 中是合法的,但几乎不是您想要的。
C++ 中的数组必须使用 delete[] 删除:
这:
没有意义 - 您更改了度数,但没有更改与其关联的数组。
我不知道你认为这是在做什么,我怀疑你也不知道。而且你有内存泄漏。
这只是一个开始——我怀疑还有更多糟糕的事情。您需要做两件事:
阅读一本有关 C++ 的书。由于您有编程经验,我建议您Accelerated C++。
忘记您的 Java 知识。正如我所说,这两种语言几乎没有任何共同点。
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.
This creates an array of size zero, which is legal in C++, but almost never what you want.
Arrays in C++ must be deleted with delete[]:
This:
makes no sense - you change the degree, but not the array it is associated with.
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.
您正在为
level
元素分配空间并放置level+1
元素...该行为未定义。You are allocating space for
degree
elements and putting theredegree+1
elements... The behavior is undefined.在
main()
函数中,int *coeff = new int[level]
为您提供一个长度level
数组,其元素索引范围为0
到level-1
(含)。在循环中,您将访问元素0
到level
(含)。这是未定义的行为,可能会也可能不会导致运行时错误等。In your
main()
function,int *coeff = new int[degree]
gives you a length-degree
array, with element indices ranging from0
todegree-1
, inclusive. In your loop, you are accessing elements0
todegree
, inclusive. This is undefined behaviour, which may or may not cause runtime errors, etc.for
循环中的语句i <= Degree
导致了这种情况。由于数组索引从0
开始,因此有效范围为0-> Degree-1
。由于您正在写入无效的内存位置,因此您的程序的行为不可预测。The statement
i <= degree
in thefor
loop is causing it. Since array indexes start from0
the valid range is0->degree-1
. Since you are writing to an invalid memory location your program is behaving unpredictably.