![](/img/trans.png)
[英]Error MSB6006 “CL.exe” exited with code 2 in compile in visual studio c++
[英]Visual studio 2017 - error MSB6006: “CL.exe” exited with code 2
使用Visual Studio 2017構建項目我遇到了這個錯誤:
錯誤MSB6006:“CL.exe”退出代碼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通過deafult將堆棧大小限制為1MB(我可能錯了),但我無法找到如何在Visual Studio 2017中手動增加它。有人可以幫助我嗎?
編輯:這是完整的輸出:
1> ------ Build build:項目:分形,配置:調試Win32 ------
1> Main.cpp 1>'C:\\ Program Files(x86)\\ Microsoft Visual中的內部編譯器錯誤
工作室\\ 2017年\\社區\\ VC \\工具\\ MSVC \\ 14.11.25503 \\ BIN \\ HostX86 \\ 86 \\ CL.EXE”
1>請在Visual C ++ 1>上選擇技術支持命令
幫助菜單,或打開技術支持幫助文件以獲取更多信息
信息1> C:\\ Program Files(x86)\\ Microsoft Visual
工作室\\ 2017年\\社區\\ Common7 \\ IDE \\ VC \\ VCTargets \\ Microsoft.CppCommon.targets(360,5):
錯誤MSB6006:“CL.exe”退出代碼2. 1>完成構建項目
“Fractal.vcxproj” - 失敗。
==========構建:0成功,1失敗,0最新,0跳過==========
new int[N] { 0 }
並不意味着“ 用零填充數組 ”,它實際上意味着通過將第一個元素設置為0來對數組進行聚合初始化,並對其余元素進行值初始化(將其余部分設置為0)。 例如,如果您編寫{ 1 }
,則會將第一個元素設置為1
但其余元素仍為0
。
既然您已經依賴於值初始化,那么您也可以從{0}
刪除0
,順便提一下也會使代碼編譯:
std::unique_ptr<int[]> fractal(new int[WIDTH*HEIGHT] {});
至於為什么你的原始代碼不能編譯 - 這顯然是Visual Studio 2017中的一個錯誤。隨意報告它 。
這是一個重現問題的最小應用程序:
int main()
{
auto test = new int[200000]{1};
delete[] test;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.