2015-11-04 60 views
0

所以我一直試圖解決這個問題幾個小時但沒有任何成功。我有一個多項式方法(set/get grade,set/get polynom的係數,打印多項式,針對給定數字集合查找每個y = f(x)等)。多項式類在調試器中運行良好,但在嘗試生成並運行時沒有運行

但是,問題是隻要多項式的等級是偶數,構建&運行就會返回奇怪的值。它的調試運行非常好。另一方面,只要檔次不均勻,對於調試和構建&運行都可以很好地工作。我在想這個問題是在分配內存的地方。這是代碼。由於

的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

即使沒有動態分配,你也會遇到類似的問題 - 'coeficienti'中最大的有效索引是'grad-1'。 – molbdnilo

+0

它也適用於3,5,7。它的測試.. – Gusti

+0

在上一個循環(帶有j <= grad'的循環)中,您正在訪問不存在的「coefficienti [grad]」,因爲您已分配通過'new int [this-> grad];'。這並不奇怪,這是崩潰的,令人驚訝的是,你聲稱它在某些情況下有效。你真的確定,你的測試沒問題嗎? – user463035818

回答

2

(由您的措辭 '畢業')度的多項式P N有N + 1個係數;

在僞碼:

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

因而此

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

分配一個詮釋過少。

偶數次多項式有奇數個係數,奇次多項式有偶數個係數。

我分配的int太少了,對於奇數級多項式來說,桶的分配太小,是在2 * sizeof(int)內存對齊的邊界處;因此,該桶可能不被使用。

這並不適用於偶數度,它們的遺漏桶位於新的對齊塊內。

這也是解釋調試和發佈模仿之間的差異;調試編譯器運行簡單地打包比釋放編譯器密度小的內存。

除此之外,您根本無需處理手動字段分配;例如std::vectorstd::valarray以安全的方式自動執行該任務。

+0

謝謝你這個完整的答案。它終於奏效了,我有了它的想法。 – Gusti

+0

yw,請記住要查找std :: vector和std :: valarray。對於類型爲「T」和「grad + 1」長度的一次運行時間大小但隨後大小固定的實或復係數序列,係數爲(T(0。),grad + 1)是一個堅實的選擇。 –

相關問題