Why did this code crash?

The following code causes a stack overflow, but I don't understand 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

+4
source share
2 answers

I would say that this is probably because 1024 * 768 * 3 2,359,296 , which is probably too large for the local stack.

Instead, you should allocate this on the heap.

+16
source

Allocating arrays of arrays as it is larger than the default Windows stack. Try resizing it and you will see that it will no longer work.

+2
source

Source: https://habr.com/ru/post/1310417/


All Articles