多项式类在调试器中运行良好,但在尝试构建和运行时则不行

时间:2015-11-04 12:39:30

标签: c++ class debugging

所以我一直试图解决这个问题几个小时但没有任何成功。我有一个多项式方法的类(设置/获得等级,设置/获取多项式系数,打印多项式,找到每个y = f(x)给定一组数字等。)

然而,问题在于,只要多项式的等级是偶数,build& run就会返回奇怪的值。它的调试运行完全正常。另一方面,每当等级不均匀时,对于调试和构建和运行都可以完美地工作。我认为这个问题是在dinamically分配内存的某个地方。这是代码。感谢

的main.cpp

    #include "poly_header.h"


int main()
{
    Poly p;
    p.AfisareValori();

    return 0;
}

poly_header.h

#include <iostream>
#include <math.h>
using namespace std;


class Poly
{
private:
    int grad;
    int* coeficienti;
    int a;
    int b;
    int dx;
public:
    Poly();
    void SetGrad();
    int GetGrad();
    void SetCoef();
    void GetCoef();
    void SetPolyn();
    void PrintPoly();
    void SetInterval();
    void SetDistanta();
    void AfisareValori();
    ~Poly();
};

poly_functions.cpp

#include "poly_header.h"

Poly::Poly()
{
    this->grad = 0;
    this->coeficienti = new int[0];
}

Poly::~Poly()
{
    delete coeficienti;
}

void Poly::SetGrad()
{
    int n;
    cout<<"Introduceti gradul dorit al polinomului: ";
    cin>>n;
    this->grad = n;
}

int Poly::GetGrad()
{
    cout<<"\n";
    return this->grad;
}

void Poly::SetCoef()
{
    int n;
    this->coeficienti = new int[this->grad];
    for(int i = 0; i <= this->grad; i++){
        cout<<"Introduceti coeficientul "<<i<<" :";
        cin>>this->coeficienti[i];
        cout<<"\n";
    }
}

void Poly::GetCoef()
{
    cout<<"\n";
    for(int i = 0; i <= this->grad; i++)
        cout<<"Coeficientul asociat termenului x^"<<i<<" este:"<<this->coeficienti[i]<<"\n";
}

void Poly::SetPolyn()
{
    SetGrad();
    SetCoef();
}

void Poly::PrintPoly()
{
    SetPolyn();
    for(int i = grad; i >= 0; i--){
        if(i == 0){
            cout<<coeficienti[i];
            break;
        }
        cout<<coeficienti[i]<<"*X^"<<i<<" + ";
    }
}

void Poly::SetInterval()
{
    int a,b;
    cout<<"Introduceti capatul din stanga al intervalului: ";
    cin>>a;
    this->a = a;
    cout<<"\n";
    cout<<"Introduceti capatul din dreapta al intervalului: ";
    cin>>b;
    this->b = b;
    cout<<"\n";
}

void Poly::SetDistanta()
{
    int dx;
    cout<<"Introduceti distanta dintre puncte: ";
    cin>>dx;
    this->dx = dx;
    cout<<"\n";
}

void Poly::AfisareValori()
{
    SetPolyn();
    SetInterval();
    SetDistanta();
    int suma;
    for(int i = a; i <= b; i+=dx){
        suma = 0;
        for(int j = 0; j <= grad; j++){
            suma += coeficienti[j] * (pow (i,j));
        }
        cout<<"Valorea polinomului in punctul "<<i<<" este "<<suma<<"\n";
    }
}

1 个答案:

答案 0 :(得分:2)

度数的多项式P(&#39; grad&#39;用你的措辞)N有N + 1个系数;

在伪代码中:

length((N, N-1, ... 1, 0)) = N + 1.

因此这个

void Poly::SetCoef()
{
    /*...*/ 
    this->coeficienti = new int[this->grad];
    /*...*/ 
)

分配一个int太少了。

偶数度的多项式具有奇数N个系数,奇数度的多项式具有偶数个系数。

我分配的int太少,对于奇数度的多项式而言,桶太小,处于2 * sizeof(int)内存对齐的边界;因此,很可能不会使用该桶。

这不会保持均匀度,它们丢失的存储桶位于新的对齐块内。

这也解释了调试和发布模拟之间的差异;调试编译器运行只是打包内存不如发布编译器密集。

除此之外,您根本不必处理人工现场分配;例如std::vectorstd::valarray以安全的方式自动执行该任务。