使用 Visual Studio 2017 构建项目时遇到此错误:
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 数组,代码编译但在我调试它时抛出异常,发出信号堆栈溢出。
减少数组的大小就解决了问题,所以看起来程序没有足够的内存空间来运行。我在谷歌上搜索了很多,发现 visual studio 默认将堆栈大小限制为 1MB(我可能错了),但我找不到如何在 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 ==========
最佳答案
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;
}
关于c++ - Visual Studio 2017 - 错误 MSB6006 : "CL.exe" exited with code 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46747649/