Consider the following code (for demonstration purposes only):
#include <iostream>
int main()
{
char pixels[4][1280][720]; // Big enough to cause a stack overflow on my machine
for (unsigned int i = 0; i < 4; i++)
{
for (unsigned int j = 0; j < 1280; j++)
{
for (unsigned int k = 0; k < 720; k++)
{
pixels[i][j][k] = i + j + k;
}
}
}
std::cout << pixels[2][640][360];
return 0;
}
According to answers on this question, the maximum stack size is set by visual studio.
Am I correct in assuming it could warn users about a potential stack overflow? (I tried this myself and didn't get a warning)
P.S: The only reason I'm asking is because I see a lot of questions on SO that could be prevented by such a warning (Yes I know not everyone SO user uses VS).
It already does, in the editions that have the /analyze
flag available:
C:\>cl /EHsc /analyze stack.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
stack.cpp
c:\stack.cpp(3) : warning C6262: Function uses '3686412' bytes of stack: exceeds /analyze:stacksize '16384'. Consider moving some data to heap.
The prefast
tool shipped with the DDK/WDK produces similar warnings.
Of course, this is a very simple static check (if the function's stack usage is above a certain threshold). It does not attempt to detect recursive calls or add up total static usage through call chains.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With