Export (0) Print
Expand All

C6262

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

This warning indicates that stack usage exceeding a preset threshold has been detected inside a function. The default stack size for this warning is 16K bytes. Stack, even in user-mode, is limited, and failure to commit a page of stack causes a stack overflow exception. The _resetstkoflw function recovers from a stack overflow condition, allowing a program to continue instead of failing with a fatal exception error. If the _resetstkoflw function is not called, there is no guard page after the previous exception. The next time that there is a stack overflow, there are no exception at all and the process terminates without warning.

To correct this warning, you can either move some data to heap, or increase the stack size. In either case, before you make changes to your code, you should consider the advantages and disadvantages of the approach you take.

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

#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:

#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);
  }
}

To correct this warning by adjusting the stack size, follow these steps:

  1. On the Project menu, click Properties.

    The Property Pages dialog box is displayed.

  2. Expand the Configuration Properties tree.

  3. Expand the C/C++ tree.

  4. Click the Command Line properties.

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

Community Additions

ADD
Show:
© 2014 Microsoft