C++内存分配错误

发布于 2024-11-06 02:10:40 字数 2418 浏览 0 评论 0原文

我是一名 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

墨小墨 2024-11-13 02:10:40

您的代码存在大量错误。 C++ 与 Java 完全不同,看起来您使用指针就好像它们就像 Java 中的引用一样,但它们显然不是。

Polynom::Polynom()
{
    this->degree = 0;
    this->coeff = new int[0];
}

这会创建一个大小为零的数组,这在 C++ 中是合法的,但几乎不是您想要的。

Polynom::~Polynom()
{
    delete coeff;
}

C++ 中的数组必须使用 delete[] 删除:

Polynom::~Polynom()
{
    delete [] coeff;
}

这:

void Polynom::setDegree(int degree)
{
    this->degree = degree;
}

没有意义 - 您更改了度数,但没有更改与其关联的数组。

void Polynom::setCoeffs(int* coeff)
{
    this->coeff = &*coeff;
}

我不知道你认为这是在做什么,我怀疑你也不知道。而且你有内存泄漏。

这只是一个开始——我怀疑还有更多糟糕的事情。您需要做两件事:

  • 阅读一本有关 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.

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.

烂人 2024-11-13 02:10:40
int* coeff = new int[degree];
int i;
for(i = 0; i <= degree; i++)

您正在为 level 元素分配空间并放置 level+1 元素...该行为未定义。

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.

耳根太软 2024-11-13 02:10:40

main() 函数中,int *coeff = new int[level] 为您提供一个长度 level 数组,其元素索引范围为0level-1(含)。在循环中,您将访问元素 0level(含)。这是未定义的行为,可能会也可能不会导致运行时错误等。

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.

枯叶蝶 2024-11-13 02:10:40

for 循环中的语句 i <= Degree 导致了这种情况。由于数组索引从 0 开始,因此有效范围为 0-> Degree-1。由于您正在写入无效的内存位置,因此您的程序的行为不可预测。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文