Skip to content

Commit b60cb77

Browse files
authored
Merge pull request #4434 from MugBergerFries/patch-3
Updated C6200
2 parents a659a4c + 81890eb commit b60cb77

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

docs/code-quality/c6200.md

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,50 @@
11
---
22
description: "Learn more about: C6200"
33
title: C6200
4-
ms.date: 11/04/2016
4+
ms.date: 08/16/2022
55
ms.topic: reference
6-
f1_keywords: ["C6200"]
6+
f1_keywords: ["C6200", "INDEX_EXCEEDS_MAX_NONSTACK", "__WARNING_INDEX_EXCEEDS_MAX_NONSTACK"]
77
helpviewer_keywords: ["C6200"]
88
ms.assetid: bbeb159b-4e97-4317-9a07-bb83cd03069a
99
---
10-
# C6200
10+
# Warning C6200
1111

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*'
1313
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
1517

1618
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.
1719

20+
Code analysis name: INDEX_EXCEEDS_MAX_NONSTACK
21+
1822
## Example
1923

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:
2125

2226
```cpp
23-
int buff[14]; // array of 0..13 elements
2427
void f()
2528
{
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;
3135
}
3236
```
3337

3438
To correct both warnings, use correct array size as shown in the following code:
3539

3640
```cpp
37-
int buff[14]; // array of 0..13 elements
3841
void f()
3942
{
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;
4549
}
4650
```

0 commit comments

Comments
 (0)