Skip to content

Commit 0cd52ee

Browse files
authored
Merge pull request #3210 from corob-msft/docs/corob/cpp-docs.zh-tw-20
Address cpp-docs.zh-tw issue 20
2 parents 3da1807 + 971c261 commit 0cd52ee

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

docs/code-quality/c6262.md

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
---
22
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
45
ms.topic: reference
56
f1_keywords: ["C6262"]
67
helpviewer_keywords: ["C6262"]
7-
ms.assetid: 3eddc74e-2b05-4159-a093-fd469fdcebbb
88
---
99
# C6262
1010

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
1212
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
1414

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.
1616

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.
1838

1939
## Example
2040

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.
2242

2343
```cpp
2444
// cl.exe /c /analyze /EHsc /W4
@@ -61,18 +81,10 @@ void f( )
6181
}
6282
```
6383

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).
7385

7486
## See also
7587

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

Comments
 (0)