Reputation: 59
Building a project with Visual Studio 2017 i came across this error:
error MSB6006: "CL.exe" exited with code 2.
here is my code:
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;
}
the problem seems to be the declaration of the fractal array:
unique_ptr<int[]> fractal(new int[WIDTH*HEIGHT]{ 0 });
if i comment that (and the other lines with the fractal variable) the code compiles just fine, and if i change the unique pointer into a normal int array the code compiles but it throws an exception when i debug it, signaling a stack overflow.
Reducing the size of the array solves the problem, so it looks like the program doesn't have enough memory space to run. I googled quite a lot and found that visual studio limits the stack size to 1MB by deafult (i might be wrong on that), but i can't find how to increase it manually in visual studio 2017. Can someone help me?
EDIT: here is the complete output:
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 ==========
Upvotes: 5
Views: 6479
Reputation: 85491
new int[N] { 0 }
does not mean "fill the array with zeroes", it actually means aggregate-initialize the array by setting the first element to 0, and value-initialize the rest (which sets the rest to 0). For example if you wrote { 1 }
, that would set the first element to 1
but the rest would still be 0
.
Since you're already relying on value-initialization, you might as well remove the 0
from {0}
, that incidentally also makes your code compile:
std::unique_ptr<int[]> fractal(new int[WIDTH*HEIGHT] {});
As to why your original code doesn't compile - it's clearly a bug in Visual Studio 2017. Feel free to report it.
Here's a minimal application to reproduce the issue:
int main()
{
auto test = new int[200000]{1};
delete[] test;
}
Upvotes: 5