|
1 | 1 | ---
|
2 | 2 | description: "Learn more about: C6200"
|
3 | 3 | title: C6200
|
4 |
| -ms.date: 11/04/2016 |
| 4 | +ms.date: 08/16/2022 |
5 | 5 | ms.topic: reference
|
6 |
| -f1_keywords: ["C6200"] |
| 6 | +f1_keywords: ["C6200", "INDEX_EXCEEDS_MAX_NONSTACK", "__WARNING_INDEX_EXCEEDS_MAX_NONSTACK"] |
7 | 7 | helpviewer_keywords: ["C6200"]
|
8 | 8 | ms.assetid: bbeb159b-4e97-4317-9a07-bb83cd03069a
|
9 | 9 | ---
|
10 |
| -# C6200 |
| 10 | +# Warning C6200 |
11 | 11 |
|
12 |
| -> warning C6200: index \<name> is out of valid index range \<min> to \<max> for non-stack buffer \<variable> |
| 12 | +> Index '*index*' is out of valid index range '*min*' to '*max*' for non-stack buffer '*parameter-name*' |
13 | 13 |
|
14 |
| -This warning indicates that an integer offset into the specified 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 non-stack array exceeds the maximum bounds of that array, potentially causing random behavior and/or crashes. |
| 15 | + |
| 16 | +## Remarks |
15 | 17 |
|
16 | 18 | One common cause of this defect is using the size of an array 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_NONSTACK |
| 21 | + |
18 | 22 | ## Example
|
19 | 23 |
|
20 |
| -The following code generates this warning because the **`for`** loop exceeds the index range: |
| 24 | +The following code generates this warning. This issue stems from the **`for`** loop exceeding the index range, attempting to access index 14 (the 15th element) when index 13 (the 14th element) is the last: |
21 | 25 |
|
22 | 26 | ```cpp
|
23 |
| -int buff[14]; // array of 0..13 elements |
24 | 27 | void f()
|
25 | 28 | {
|
26 |
| - for (int i=0; i<=14;i++) // i exceeds the index |
27 |
| - { |
28 |
| - buff[i]= 0; // warning C6200 |
29 |
| - // code... |
30 |
| - } |
| 29 | + int* buff = new int[14]; // array of 0..13 elements |
| 30 | + for (int i = 0; i <= 14; i++) // i exceeds the index |
| 31 | + { |
| 32 | + buff[i] = 0; // warning C6200 |
| 33 | + } |
| 34 | + delete[] buff; |
31 | 35 | }
|
32 | 36 | ```
|
33 | 37 |
|
34 | 38 | To correct both warnings, use correct array size as shown in the following code:
|
35 | 39 |
|
36 | 40 | ```cpp
|
37 |
| -int buff[14]; // array of 0..13 elements |
38 | 41 | void f()
|
39 | 42 | {
|
40 |
| - for ( int i=0; i < 14; i++) // loop stops when i < 14 |
41 |
| - { |
42 |
| - buff[i]= 0; // initialize buffer |
43 |
| - // code... |
44 |
| - } |
| 43 | + int* buff = new int[14]; // array of 0..13 elements |
| 44 | + for (int i = 0; i < 14; i++) // i == 13 on the final iteration |
| 45 | + { |
| 46 | + buff[i] = 0; // initialize buffer |
| 47 | + } |
| 48 | + delete[] buff; |
45 | 49 | }
|
46 | 50 | ```
|
0 commit comments