Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning message regarding stack size

I use Visual Studio 2010 with Code Analysis activated. In my code there's a line allocating some memory in a function:

TCHAR someString[40000]; 

The code analysis throws a warning message:

warning C6262: Function uses '40000' bytes of stack: exceeds /analyze:stacksize'16384'. Consider moving some data to heap

I wonder if I should take the warning serious. Do I have to face some real trouble if I allocate some memory on the stack > 16384? Or is it just a general warning message which reminds me that I have to take care for my stack size in general? As far as I know the default stack size is 1MB (if you use Visual Studio).

like image 300
MichaelXanadu Avatar asked Apr 24 '14 12:04

MichaelXanadu


People also ask

What happens when stack space limit is reached?

If you do, exceeding the stack limit is usually considered a segmentation violation, and systems with enough memory management to detect it will send a SIGSEGV when it happens.

Does a stack have a size limit?

The stack size limit is the maximum size of the stack for a process, in units of 1024 bytes. The stack is a per-thread resource that has unlimited hard and soft limits.

Why is the stack limited in size?

If the stack would be allowed to grow arbitrarily large, these errors (like infinite recursion) would be caught very late, only after the operating systems resources are exhausted. This is prevented by setting an arbitrary limit to the stack size.

What determines size of stack?

It's allocated by the OS on execution of a process. FWIW, stack size is not fixed, it expands at runtime as necessary, but stack limit is fixed.


1 Answers

Admittedly, that message can be confusing since VS (project properties) does report that the default is 1M. However, if you look at the text of the warning, you'll note that the limit is actually 16k for Code Analysis. Follow the steps at the bottom of that link to correct the warning.

like image 70
rrirower Avatar answered Sep 24 '22 05:09

rrirower