본문 바로가기

C/C++

C6262 Excessive Stack Usage (스택 사이즈 초과 사용)

C6262  Excessive Stack Usage (스택 사이즈 초과 사용)

 

Visual Studio 11에서 빌드하다가 위와 같은 경고 만났다

디폴트 스택 사이즈는 16K Byte 인데이를 초과했을 경우 발생하는 경고이다.

경고이긴 한데현재 진행 중인 프로젝트가 가끔 디버그에서 죽는 이유가 아닐까 싶어서 소스를 수정하기로 한다.

디폴트로 세팅되어 있는 스택 사이즈는 16K Byte 이며이를 넘어설 경우 stack overflow exception이 발생할 수 있다_resetstkoflw  함수를 사용하여 stack overflow 상태를 회복시켜, fatal exception error를 뱉으며 죽어가야 할 프로그램을 살리는 방법이 있긴 하나보다

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.

** msdn에서는 2가지 방법을 제시한다.

1.     힙 메모리를 할당해서 사용하기

2.     스택 사이즈를 늘려주기

하나씩 살펴보자. 

1.     힙 메모리를 할당해서 사용하기

 아래 함수는 로컬 버퍼에 16K byte를 할당하며 int 변수 i에 4byte를 할당해서, 16K byte를 넘게 스택 사이즈를 사용하고 있다.

#include <windows.h>

#define MAX_SIZE 16382

 

void f( )

{

  int i;

  char buffer[MAX_SIZE];

 

  i = 0;

  buffer[0]='\0';

 

  // code...

}

위 소스는 아래와 같이 힙 메모리를 사용하도록 변경한다.

 

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

  }

}

 

2.  스택 사이즈를 늘려주기

Visual studio를 사용하여 스택 사이즈를 프로젝트 옵션에서 변경한다.

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.

 

<출처>

Msdn :

http://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=EN-US&k=k(C6262)&rd=true