|
1 | 1 | ---
|
2 | 2 | title: C6262
|
3 |
| -ms.date: 11/04/2016 |
| 3 | +description: "Visual Studio C++ Code Analysis warning C6262 description and resolution." |
| 4 | +ms.date: 10/14/2020 |
4 | 5 | ms.topic: reference
|
5 | 6 | f1_keywords: ["C6262"]
|
6 | 7 | helpviewer_keywords: ["C6262"]
|
7 |
| -ms.assetid: 3eddc74e-2b05-4159-a093-fd469fdcebbb |
8 | 8 | ---
|
9 | 9 | # C6262
|
10 | 10 |
|
11 |
| -> warning C6262: Function uses *constant1* bytes of stack: exceeds /analyze:stacksize *constant2*. Consider moving some data to heap |
| 11 | +> warning C6262: Function uses *constant_1* bytes of stack: exceeds /analyze:stacksize *constant_2*. Consider moving some data to heap |
12 | 12 |
|
13 |
| -This warning indicates that stack usage that exceeds a preset threshold (*constant2*) 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. |
| 13 | +## Remarks |
14 | 14 |
|
15 |
| -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 *constant2*, but increasing it introduces a risk that an error will not be reported. |
| 15 | +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. |
16 | 16 |
|
17 |
| -For kernel-mode code—for example, in driver projects—the value of *constant2* 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. |
| 17 | +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. |
| 18 | + |
| 19 | +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. |
| 20 | + |
| 21 | +## Adjust the stack size to suppress the warning |
| 22 | + |
| 23 | +You can use the [`/analyze:stacksize`](../build/reference/analyze-code-analysis.md) command-line option to change the value for *constant_2*, but increasing it introduces a risk that an error may not be reported. |
| 24 | + |
| 25 | +### To suppress the warning on the command line |
| 26 | + |
| 27 | +- 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`*. |
| 28 | + |
| 29 | +### To suppress the warning in the IDE |
| 30 | + |
| 31 | +1. In the Visual Studio IDE, select the project in the **Solution Explorer** window. |
| 32 | + |
| 33 | +1. On the menu bar, choose **Project** > **Properties**. |
| 34 | + |
| 35 | +1. In the **Property Pages** dialog box, select the **Configuration Properties** > **C/C++** > **Command Line** property page. |
| 36 | + |
| 37 | +1. 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. |
18 | 38 |
|
19 | 39 | ## Example
|
20 | 40 |
|
21 |
| -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. |
| 41 | +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. |
22 | 42 |
|
23 | 43 | ```cpp
|
24 | 44 | // cl.exe /c /analyze /EHsc /W4
|
@@ -61,18 +81,10 @@ void f( )
|
61 | 81 | }
|
62 | 82 | ```
|
63 | 83 |
|
64 |
| -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`](../standard-library/shared-ptr-class.md), [`unique_ptr`](../standard-library/unique-ptr-class.md), and [`vector`](../standard-library/vector.md). For more information, see [Smart Pointers](../cpp/smart-pointers-modern-cpp.md) and [C++ Standard Library](../standard-library/cpp-standard-library-reference.md). |
65 |
| - |
66 |
| -### To correct this warning by adjusting the stack size |
67 |
| - |
68 |
| -1. On the menu bar, choose **Project** > **Properties**. |
69 |
| - |
70 |
| -1. In the **Property Pages** dialog box, select the **Configuration Properties** > **C/C++** > **Command Line** property page. |
71 |
| - |
72 |
| -1. In **Additional options**, add *`/analyze:stacksize32768`*. |
| 84 | +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`](../standard-library/shared-ptr-class.md), [`unique_ptr`](../standard-library/unique-ptr-class.md), and [`vector`](../standard-library/vector.md). For more information, see [Smart Pointers](../cpp/smart-pointers-modern-cpp.md) and [C++ Standard Library](../standard-library/cpp-standard-library-reference.md). |
73 | 85 |
|
74 | 86 | ## See also
|
75 | 87 |
|
76 |
| -- [`/STACK` (Stack Allocations)](../build/reference/stack-stack-allocations.md) |
77 |
| -- [`_resetstkoflw`](../c-runtime-library/reference/resetstkoflw.md) |
78 |
| -- [How to: Use Native Run-Time Checks](/visualstudio/debugger/how-to-use-native-run-time-checks) |
| 88 | +[`/STACK` (Stack allocations)](../build/reference/stack-stack-allocations.md)\ |
| 89 | +[`_resetstkoflw`](../c-runtime-library/reference/resetstkoflw.md)\ |
| 90 | +[How to: Use native run-time checks](/visualstudio/debugger/how-to-use-native-run-time-checks) |
0 commit comments