Bewerken

Delen via


Warning C6262

Function uses constant_1 bytes of stack: exceeds /analyze:stacksize constant_2. Consider moving some data to heap

Remarks

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 can't be increased. Try to aggressively limit stack use in kernel-mode code.

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.

For kernel-mode code—for example, in driver projects—the value of constant_2 is set to 1 KB. Well-written drivers should have 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.

Code analysis name: EXCESSIVESTACKUSAGE

Adjust the stack size to suppress the warning

You can use the /analyze:stacksize command-line option to change the value for constant_2, but increasing it introduces a risk that an error may not be reported.

To suppress the warning on the command line

  • Add the /analyze:stacksize <new-size> option to the compiler command line. Use a value for <new-size> that's larger than constant_1. For example, if constant_1 is 27180, you might enter /analyze:stacksize 32768.

To suppress the warning in the IDE

  1. In the Visual Studio IDE, select the project in the Solution Explorer window.

  2. On the menu bar, choose Project > Properties.

  3. In the Property Pages dialog box, select the Configuration Properties > C/C++ > Command Line property page.

  4. In Additional options, add /analyze:stacksize <new-size>, where <new-size> is larger than constant_1. For example, if constant_1 is 27180, you might enter /analyze:stacksize 32768. Choose OK to save your changes.

Example

The following code generates this warning because char buffer requires 16,382 bytes on the stack, and the local integer variable i requires 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 has many pitfalls, such as memory leaks and exceptions. To avoid these kinds of leaks and exception problems altogether, use the mechanisms that are provided by the C++ Standard Library (STL). These include shared_ptr, unique_ptr, and vector. For more information, see Smart Pointers and C++ Standard Library.

See also

/STACK (Stack allocations)
_resetstkoflw
How to: Use native run-time checks