2012-12-26 57 views
2

我試圖將正確工作在Windows中的庫移植到Linux。將代碼從Windows移植到Linux時刪除陣列時出錯

在這幾行代碼我得到一個錯誤:

long* permutation = new long[result->getGeneListCount()]; 
for(long i=0; i<result->getGeneListCount(); i++) 
     permutation[i]=i; 
Util::ArrayUtil::DurstenfeldArrayPermutation<long>(permutation, result->getGeneListCount()); 

//result->PerformGenePermutation(permutation); 
std::cout << "Just skipped the permutation" << std::endl; 

delete[] permutation; 

的錯誤似乎對我來說,在刪除過程中發生。我知道,因爲我已經評論了PerformGenePermutation(),我可以簡單地評論其他行,但類似的問題可能會在其他代碼中再次出現,所以我想了解錯誤。

錯誤輸出,我得到的是:

*** glibc detected *** /usr/lib/jvm/java-7-oracle/bin/java: munmap_chunk(): invalid pointer: 0x09f287f8 *** 

誰能幫助我,好嗎?

請問,如果您需要進一步的細節。

+1

是否有可能'result-> getGeneListCount()'retuns'0'? – andre

+3

'permutation'指針是否改變了對'DurstenfeldArrayPermutation'的調用?你可以通過在調用之前和之後打印指針值('printf(「%p \ n」,permutation);')來驗證它嗎? DurstenfeldArrayPermutation'自己釋放指針嗎? – ulidtko

+2

'DurstenfeldArrayPermutation'是否通過引用獲取其第一個參數?如果是這樣,它可能試圖重新分配它,這在某些平臺上可能沒有問題(其中'malloc'和'new []'用戶具有相同的底層分配器),但是在其他平臺上則沒有。 –

回答

2

給定的代碼&信息不足以明確了問題的原因,但你可以做到以下幾點:

與替換代碼

long* permutation = new long[result->getGeneListCount()]; 
for(long i=0; i<result->getGeneListCount(); i++) 
     permutation[i]=i; 
Util::ArrayUtil::DurstenfeldArrayPermutation<long>(permutation, result->getGeneListCount()); 

//result->PerformGenePermutation(permutation); 
std::cout << "Just skipped the permutation" << std::endl; 

delete[] permutation; 

std::vector<long> permutation(result->getGeneListCount()); 
for(long i=0; i<long(permutation.size()); i++) 
     permutation[i]=i; 
Util::ArrayUtil::DurstenfeldArrayPermutation<long>(&permutation.at(0), permutation.size()); 

//result->PerformGenePermutation(permutation); 
std::cout << "Just skipped the permutation" << std::endl; 

//delete[] permutation; 

請注意,delete已被刪除,因爲std::vector會自動爲您執行此操作。

如果這現在從std::vector::at引發範圍錯誤的異常,那麼您知道該大小可能爲零。無論如何,你現在可以非常簡單地檢查你的調試器。更重要的是,如果不是會拋出一個異常,那麼你知道這個代碼一切正常並且很好(因爲std::vector是可靠的),所以問題出現在其他地方。

不幸的是,這個帖子太長,不能發表評論,但它不是真的答案。這是SO的問題。由於它是爲純粹的答案設計的,因此不支持通用幫助

+0

嗯,它的工作。沒有例外被拋出。現在對我來說已經足夠了:) – Aslan986