Dela via


C6201

warning C6201: buffer overrun for <variable>, which is possibly stack allocated: index <name> is out of valid index range <min> to <max>

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.

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.

Example

The following code generates this warning because the array index is out of the valid range:

void f( )
{
  int buff[25];
  for (int i=0; i <= 25; i++) // i exceeds array bound
  {
    buff[i]=0; // initialize i
    // code ...
  }
}

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

void f( )
{
  int buff[25];
  for (int i=0; i < 25; i++)
  {
    buff[i]=0; // initialize i
    // code ...
  }
}