2011-05-31 40 views
3

下面一段代碼,用於生成素數,編譯和運行運行調試時如預期的,但似乎總是崩潰在Release模式進行構建時,墨棒:的Visual C++接頭崩潰(VS2008 SP1)

#include <vector> 
#include <algorithm> 
#include <cstdlib> 
#include <iostream> 

template<typename T> class PrimeGen { 
    struct Elim { 
     T p ; 
     T e ; 
    } ; 

    class Elim_cmp { 
    public: 
     bool operator()(const Elim& e1, const Elim& e2) { return e1.e > e2.e; } 
    } ; 

    std::vector<Elim> elim_heap ; 
    T last ; 

public: 
    PrimeGen() { 
     Elim e0 = { 2, 4 } ; 
     elim_heap.reserve(1024) ; 
     elim_heap.push_back(e0) ; 
     last = 2 ; 
    } 

    T next() { 
     T n = last ; 
     bool prime; 
     do { 
      n ++ ; 
      prime = true ; 
      while(n == elim_heap.front().e) { 
       if(prime) prime = false ; 
       std::pop_heap(elim_heap.begin(), elim_heap.end(), Elim_cmp()) ; 
       Elim& elim = elim_heap.back() ; 
       elim.e = elim.p + n ; 
       std::push_heap(elim_heap.begin(), elim_heap.end(), Elim_cmp()) ; 
      } 
     } while(!prime) ; 
     Elim e = { n, 2*n } ; 
     elim_heap.push_back(e) ; 
     std::push_heap(elim_heap.begin(), elim_heap.end(), Elim_cmp()) ; 
     return last = n ; 
    } 
} ; 

int main() 
{ 
    using namespace std ; 
    PrimeGen<unsigned int> pgen ; 
    for(int i=0; i<100; i++) { 
     cout << pgen.next() << endl ; 
    } 
    system("pause") ; 
} 

任何人都可以看到爲什麼會發生這種情況嗎?或者我遇到過編譯器而不是代碼難以指責的罕見情況之一?

我得到的錯誤如下:

error PRJ0002 : Error result -1073741819 returned from 'C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\mt.exe'. 

如果別人誰在運行Visual Studio會花時間去嘗試和釋放模式構建代碼(我已刪除任何外部依賴,所以它應該只是c & p)我會非常感激。

UPDATE:進一步的審查,似乎mt.exe在發佈模式下仍然崩潰,即使對於簡單的「Hello World」代碼,所以必須有我的安裝問題。我將重新安裝所有希望解決此問題的應用程序。版主,隨時關閉這個問題。

+1

使用VC++ 2010 SP1爲x86和x64版本構建乾淨利落。 – ildjarn 2011-05-31 20:33:57

+1

我能夠編譯/鏈接(VS 2008 9.0.21022.8)沒有問題。我沒有想到,因爲我有一些多項目解決方案經常會導致鏈接器崩潰,但是這對我來說是不可重複的 - 通常第二個F7會在沒有進一步錯誤的情況下首次構建失敗的錯誤。 – mah 2011-05-31 20:34:39

+1

mt.exe只是嵌入清單。也許有些文件最終被鎖定?你可以嘗試清理解決方案(確保所有生成的文件都不存在),然後重建? – Bart 2011-05-31 20:35:28

回答

0

某些程序(如防病毒程序)在鏈接程序試圖嵌入清單文件的同時掃描file.exe。