tags:

views:

188

answers:

2

The following code causes a stack overflow but I don't see why...

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "start";
    char bmp[1024][768][3];
for (int p = 0; p < 9000; ++p)
{
    for(int i = 0; i < 1024; ++i)
    {
        for(int j = 0; j < 768; ++j)
        {
            bmp[i][j][0] = 20;
        }

    }
}

cout << "Stop";


    return 0;
}

Thanks

+16  A: 

I would say it is likely because 1024 * 768 * 3 is 2,359,296 which is probably too big for the local stack.

You should instead allocate that on the heap.

karoberts
ok that makes sense
Milo
The default stack is 1MB on Windows. http://msdn.microsoft.com/en-us/library/ms686774.aspx
Jeff Yates
And prefer a std::vector
stefaanv
@steaanv: Why use a std::vector for something as simple? You don't really need it.
nico
+2  A: 

Allocating the int array crashes since it's bigger than the default Windows stack. Try changing it's size and you'll see that it crashes no more.

Adal