Share via


C6262

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

warning C6262: Function uses <constant> bytes of stack: exceeds /analyze:stacksize<constant 2>. Consider moving some data to heap

This warning indicates that stack usage that exceeds a preset threshold (constant 2) has been detected in a function. The default stack frame size for this warning is 16 KB for user mode, 1 KB for kernel mode. Stack—even in user mode—is limited, and failure to commit a page of stack causes a stack overflow exception. Kernel mode has a 12 KB stack size limit, which cannot be increased; therefore, kernel-mode code should aggressively limit stack use.

To correct the problem behind this warning, you can either move some data to the heap or to other dynamic memory. In user mode, one large stack frame may not be a problem—and this warning may be suppressed—but a large stack frame increases the risk of a stack overflow. (A large stack frame might occur if the function uses the stack heavily or is recursive.) The total stack size in user mode can be increased if stack overflow actually occurs, but only up to the system limit. You can use the /analyze command-line option to change the value for <constant 2>, but increasing it introduces a risk that an error will not be reported.

For kernel-mode code—for example, in driver projects—the value of <constant 2> is set to 1 KB. Well-written drivers should have very few functions that approach this value, and changing the limit downward may be desirable. The same general techniques that are used for user-mode code to reduce the stack size can be adapted to kernel-mode code.

Example

The following code generates this warning because char buffer allocates 16,382 bytes, and the local integer variable i allocates another 4 bytes, which together exceed the default stack size limit of 16 KB.

// cl.exe /c /analyze /EHsc /W4  
#include <windows.h>  
#define MAX_SIZE 16382  
  
void f( )  
{  
    int i;  
    char buffer[MAX_SIZE];  
  
    i = 0;  
    buffer[0]='\0';  
  
    // code...  
}  
  

The following code corrects this warning by moving some data to heap.

// cl.exe /c /analyze /EHsc /W4  
#include <stdlib.h>     
#include <malloc.h>  
#define MAX_SIZE 16382  
  
void f( )  
{  
    int i;  
    char *buffer;  
  
    i = 0;  
    buffer = (char *) malloc( MAX_SIZE );  
    if (buffer != NULL)   
    {  
        buffer[0] = '\0';  
        // code...  
        free(buffer);  
    }  
}  
  

The use of malloc and free have many pitfalls in terms of memory leaks and exceptions. To avoid these kinds of leaks and exception problems altogether, use the mechanisms that are provided by the C++ Standard Template Library (STL). These include shared_ptr, unique_ptr, and vector. For more information, see Smart Pointers and C++ Standard Library.

To correct this warning by adjusting the stack size

  1. On the menu bar, choose Project, Properties.

    The Property Pages dialog box is displayed.

  2. Expand Configuration Properties.

  3. Expand C/C++.

  4. Select Command Line properties.

  5. In Additional options, add /analyze:stacksize16388.

See Also

/STACK (Stack Allocations)
_resetstkoflw
How to: Use Native Run-Time Checks