2017-10-14 325 views
3

建設與Visual Studio 2017年一個項目,我遇到了這個錯誤退出:的Visual Studio 2017年 - 錯誤MSB6006: 「CL.EXE」 與代碼2

error MSB6006: "CL.exe" exited with code 2.

這裏是我的代碼:

int main() 
{ 
    const int WIDTH=800; 
    const int HEIGHT=600; 

    Bitmap bitmap(WIDTH, HEIGHT); 

    unique_ptr<int[]> histogram(new int[Mandelbrot::MAX_ITERATIONS + 1]{ 0 }); 

    unique_ptr<int[]> fractal(new int[WIDTH*HEIGHT]{ 0 }); 
    //int fractal[WIDTH*HEIGHT]{ 0 }; 

    for (int y = 0; y < HEIGHT; y++) { 
     for (int x = 0; x < WIDTH; x++) { 
      double xFractal = (x - WIDTH/2 - 200)*2.0/HEIGHT; 
      double yFractal = (y - HEIGHT/2)*2.0/HEIGHT; 

      int iterations = Mandelbrot::getIterations(xFractal, yFractal); 
      if (iterations != Mandelbrot::MAX_ITERATIONS) { 
       histogram[iterations]++; 
      } 
      fractal[y*WIDTH + x] = iterations; 
      uint8_t color = 256 * (double)iterations/Mandelbrot::MAX_ITERATIONS; 
      color = color*color*color; 
      bitmap.setPixels(x, y, color, color, color); 
     } 
    } 

    bitmap.write("Mandelbrot.bmp"); 
    return 0; 
} 

的問題似乎是分形陣列的聲明:

unique_ptr<int[]> fractal(new int[WIDTH*HEIGHT]{ 0 }); 

,如果我是(和評論其他線路與分形變量)的代碼編譯就好了,如果我將獨特指針更改爲普通的int數組,代碼將編譯,但是當我調試它時,會引發異常,表示出現堆棧溢出。

減小數組的大小解決了問題,所以看起來程序沒有足夠的內存空間來運行。我GOOGLE了很多,發現visual studio限制堆棧大小爲1MB的deafult(我可能是錯誤的),但我無法找到如何在Visual Studio 2017手動增加它。有人可以幫助我嗎?

編輯:這裏是完整的輸出:

1>------ Build started: Project: Fractal, Configuration: Debug Win32 ------

1>Main.cpp 1>INTERNAL COMPILER ERROR in 'C:\Program Files (x86)\Microsoft Visual

Studio\2017\Community\VC\Tools\MSVC\14.11.25503\bin\HostX86\x86\CL.exe'

1> Please choose the Technical Support command on the Visual C++ 1>

Help menu, or open the Technical Support help file for more

information 1>C:\Program Files (x86)\Microsoft Visual

Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.CppCommon.targets(360,5):

error MSB6006: "CL.exe" exited with code 2. 1>Done building project

"Fractal.vcxproj" -- FAILED.

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

+0

題外話:我很困惑,爲什麼你使用'的unique_ptr 的''不是的std ::矢量'(或'的std ::數組「,因爲你的大小可以編譯時間常量) – UnholySheep

+2

你向我們展示的錯誤的行是最少的信息。請將* full *和* complete *錯誤消息複製粘貼到問題主體中。 –

+0

使用完整的錯誤消息進行編輯。我仍然是一名初學者,我正在學習一門課程,老實說,我使用了獨特的指針,因爲他們在課程中使用過。我剛剛嘗試使用向量,它的工作原理,而數組引發此異常:「在Fractal.exe 0x01393089未處理的異常:0xC00000FD:堆棧溢出(參數:0x00000000,0x00482000)。我現在很困惑,爲什麼這個矢量工作? –

回答

2

new int[N] { 0 }並不意味着「填充數組用零」時,其實際上意味着骨料初始化由所述第一元素設置爲0的數組,並對其餘的值進行初始化(其餘設置爲0)。例如,如果您編寫了{ 1 },則會將第一個元素設置爲1,但其他元素仍將爲0

既然你已經依靠值初始化,您不妨在0{0}刪除,這也順便讓你的代碼編譯:

std::unique_ptr<int[]> fractal(new int[WIDTH*HEIGHT] {}); 

至於爲什麼你原來的代碼沒有按不會編譯 - 這顯然是Visual Studio 2017中的一個錯誤。請隨時致電report it

這裏有一個最小的應用程序來重現問題:

int main() 
{ 
    auto test = new int[200000]{1}; 
    delete[] test; 
} 
相關問題