简体   繁体   English

数组周围的堆栈已损坏

[英]Stack around the array is corrupted

I am using Visual Studios 2015, and I ran into a problem. 我正在使用Visual Studios 2015,但遇到了问题。 Run-Time Check Failure #2 - Stack around the variable 'myArray' was corrupted. 运行时检查失败#2-变量'myArray'周围的堆栈已损坏。 I am not sure where in the program that could cause some sort of corruption to my array. 我不确定程序中的哪个位置可能导致阵列损坏。 But when I did calculations involving manipulating the array, several numbers turned to 0.0000 instead of what they were originally. 但是当我进行涉及操纵数组的计算时,有几个数字变为0.0000,而不是最初的数字。 Could someone help? 有人可以帮忙吗?

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
   double xmin, xmax;
   const int POINTS = 20;
   const double PI = 3.1416;
   double increments;
   double range, mean;
   double total = 0;
   double myArray[POINTS];
   double number = myArray[0];
   double mode = number;
   int count = 1;
   int countMode = 1;

   cout << "Enter in a value for the minimum x value: ";
   cin >> xmin;
   cout << "Enter in a value for the maximum x value: ";
   cin >> xmax;

   if (xmin < 0)
      increments = (abs(xmin) + xmax) / POINTS;
   else
      increments = (xmax - xmin) / POINTS;

   int i = 0;
   double x = xmin + increments * i;
   double minimum = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
   double maximum = myArray[19];

   cout << setw(15) << "x |" << setw(15) << "f(x)" << endl;
   cout << setw(32) << setfill('-') << " " << endl;
   cout << setfill(' ');

   for (i = 0; i <= POINTS; i++)
   {
      myArray[i] = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
      x = xmin + increments * i;
      cout << fixed << showpos << setw(15) << setprecision(2) << x <<  setw(15) << setprecision(4) << myArray[i] << endl;

      if (myArray[i] <= minimum)
         minimum = myArray[i];
      if (myArray[i] > maximum)
         maximum = myArray[i];
   }

   cout << endl;
   range = maximum - minimum;
   for (int count = 0; count <= POINTS; count++)
   {
      total += myArray[count];
   }
   mean = total / POINTS;

   int temp;
   bool swap;
   do
   {
      swap = false;
      for (int i = 0; i < POINTS - 1; i++)
      {
         if (myArray[i] > myArray[i + 1])
         {
            temp = myArray[i];
            myArray[i] = myArray[i + 1];
            myArray[i + 1] = temp;
            swap = true;
       }
      }
   } while (swap);


   for (int i = 0; i <= POINTS; i++)
   {
      if (myArray[i] == number)
      {
         count++;
      }
      else
      {
         if (count > countMode)
         {
            countMode = count;
            mode = number;
         }
         count = 1;
         number = myArray[i];
      }
   }

   cout << "The maximum value is: " << maximum << endl;
   cout << "The minimum value is: " << minimum << endl;
   cout << "The range is: " << range << endl;
   cout << "The mean value is: " << mean << endl;
   cout << "The median value is: " << (myArray[9] + myArray[10]) / 2  <<  endl;
   cout << "The mode value is: " << mode << endl;

   for (i = 0; i <= POINTS; i++)
      cout << myArray[i] << endl;

   system("pause");
   return 0;
}  
double myArray[POINTS];

myArray is an array of 20 doubles - myArray[0] through myArray[19] . myArray是20个双精度数组myArray[0]myArray[19]

for (i = 0; i <= POINTS; i++)
{
    myArray[i] = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);

This sets myArray[0] through myArray[20] . 这将通过myArray[20]设置myArray[0] myArray[20] Accessing myArray[20] is a not allowed, because that is the 21st element of a 20-element array. 不允许访问myArray[20] ,因为那是20个元素的数组的第21个元素。

Note that the compiler won't always be nice enough to detect this problem for you. 请注意,编译器并不总是足够好为您检测到此问题。 Visual C++ is doing you a favour here by causing the program to crash, believe it or not. Visual C ++通过使程序崩溃(不管您相信与不相信)都可以帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM