|
1 | 1 | ---
|
2 |
| -description: "Learn more about: C6201" |
3 |
| -title: C6201 |
4 |
| -ms.date: 11/04/2016 |
| 2 | +description: "Learn more about: Warning C6201" |
| 3 | +title: Warning C6201 |
| 4 | +ms.date: 09/28/2022 |
5 | 5 | ms.topic: reference
|
6 |
| -f1_keywords: ["C6201"] |
| 6 | +f1_keywords: ["C6201", "INDEX_EXCEEDS_MAX", "__WARNING_INDEX_EXCEEDS_MAX"] |
7 | 7 | helpviewer_keywords: ["C6201"]
|
8 | 8 | ms.assetid: eefbbd77-007c-4f28-95f6-6de5ee6a27db
|
9 | 9 | ---
|
10 |
| -# C6201 |
| 10 | +# Warning C6201 |
11 | 11 |
|
12 |
| -> warning C6201: buffer overrun for \<variable>, which is possibly stack allocated: index \<name> is out of valid index range \<min> to \<max> |
| 12 | +> Index '*index-name*' is out of valid index range '*minimum*' to '*maximum*' for possibly stack allocated buffer '*variable*' |
13 | 13 |
|
14 |
| -This warning indicates that an integer offset into the specified stack array exceeds the maximum bounds of that array. This defect might cause random behavior or crashes. |
| 14 | +This warning indicates that an integer offset into the specified stack array exceeds the maximum bounds of that array. It may potentially cause stack overflow errors, random behavior, or crashes. |
| 15 | + |
| 16 | +## Remarks |
15 | 17 |
|
16 | 18 | One common cause of this defect is using an array's size as an index into the array. Because C/C++ array indexing is zero-based, the maximum legal index into an array is one less than the number of array elements.
|
17 | 19 |
|
| 20 | +Code analysis name: INDEX_EXCEEDS_MAX |
| 21 | + |
18 | 22 | ## Example
|
19 | 23 |
|
20 |
| -The following code generates this warning because the array index is out of the valid range: |
| 24 | +The following code generates warning C6201. The **`for`** loop condition exceeds the valid index range for `buff` when it sets `i` to 14, which is one element past the end: |
21 | 25 |
|
22 | 26 | ```cpp
|
23 |
| -void f( ) |
| 27 | +void f() |
24 | 28 | {
|
25 |
| - int buff[25]; |
26 |
| - for (int i=0; i <= 25; i++) // i exceeds array bound |
27 |
| - { |
28 |
| - buff[i]=0; // initialize i |
29 |
| - // code ... |
30 |
| - } |
| 29 | + int buff[14]; // array of 0..13 elements |
| 30 | + for (int i = 0; i <= 14; i++) // i == 14 exceeds the bounds |
| 31 | + { |
| 32 | + buff[i] = 0; // initialize buffer |
| 33 | + } |
31 | 34 | }
|
32 | 35 | ```
|
33 | 36 |
|
34 |
| -To correct both warnings, use the correct array size as shown in the following code: |
| 37 | +To correct the warning, make sure the index stays in bounds. The following code shows the corrected loop condition: |
35 | 38 |
|
36 | 39 | ```cpp
|
37 |
| -void f( ) |
| 40 | +void f() |
38 | 41 | {
|
39 |
| - int buff[25]; |
40 |
| - for (int i=0; i < 25; i++) |
41 |
| - { |
42 |
| - buff[i]=0; // initialize i |
43 |
| - // code ... |
44 |
| - } |
| 42 | + int buff[14]; // array of 0..13 elements |
| 43 | + for (int i = 0; i < 14; i++) // i == 13 on the final iteration |
| 44 | + { |
| 45 | + buff[i]= 0; // initialize buffer |
| 46 | + } |
45 | 47 | }
|
46 | 48 | ```
|
0 commit comments