2011-05-09 104 views
1

我是一個Java程序員,但現在我必須寫的代碼一點點C++。幾年前,我學習了C++的基礎知識,所以我不太適合。C++的malloc錯誤

我寫它描述了多項式有點類。那就是:

#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),然後給係數,程序不打印polynome並返回以下錯誤:

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. 

爲什麼會出現這種情況?我沒有爲陣列分配足夠的內存?我搜索了這個錯誤,並偶然發現了this page,但是那裏提到的解決方案對我沒有多大幫助。

也許你可以看到在我的代碼的另一個問題?我將衷心感謝您的幫助。

在此先感謝。

+0

你能張貼的頭文件? – 2011-05-09 10:33:31

+2

你應該做'刪除在析構函數[] coeff';或者更好的是,使用'的std ::矢量'(這將擺脫你的'degree'成員,也是如此)。 – 2011-05-09 10:40:34

+0

另外,刪除poly; – 2011-05-09 10:45:00

回答

1

在你main()功能,int *coeff = new int[degree]給你一個長度 - degree數組,元素索引範圍從0degree-1,包容性。在你的循環,你正在訪問的元素0degree,包容性。這是不確定的行爲,這可能會或可能不會導致運行錯誤等

+0

謝謝你的回覆。這確實是該死的索引。哦,我贏得了學生證。我是C++新手yeeeey。我感覺就像幾年前,當我開始編程時。 :-) – Darie 2011-05-10 17:52:45

4
int* coeff = new int[degree]; 
int i; 
for(i = 0; i <= degree; i++) 

您要爲degree要素分配空間,並把有degree+1元素...行爲是不確定的。

1

for循環語句i <= degree原因造成的。由於數組索引從0開始,因此有效範圍是0->degree-1。由於您正在寫入無效的內存位置,因此您的程序行爲不可預測。

12

有廣闊的量你的代碼錯誤。 C++不是Java,它似乎是使用指針,就好像它們像Java中的引用一樣,它們強調的不是。

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

這將創建一個大小爲零的數組,這在C++中是合法的,但幾乎從不是您想要的。

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

此:在C++

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

數組必須用delete []刪除

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

是沒有意義的 - 你改變的程度,而不是陣列它所關聯用。

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

我不知道你在做什麼,我懷疑你也沒有。你有一個內存泄漏。

這只是個開始 - Isuspect還有更多壞的東西。你需要做兩件事:

  • 在C++上閱讀一本書。由於您有編程經驗,我推薦Accelerated C++

  • 忘掉你的Java知識。正如我所說,這兩種語言幾乎沒有任何共同之處。

+0

用於'delete []'的+1。 – Naveen 2011-05-09 10:42:59

+0

'這個 - > _係數=&* _係數;'...我懷疑什麼OP試圖做的是設置內部指針'coeff'到一個新的地址?在這種情況下'this-> coeff = coeff;'會很好。 – Dennis 2011-05-09 10:43:53