簡體   English   中英

數組周圍的堆棧已損壞

[英]Stack around the array is corrupted

我正在使用Visual Studios 2015,但遇到了問題。 運行時檢查失敗#2-變量'myArray'周圍的堆棧已損壞。 我不確定程序中的哪個位置可能導致陣列損壞。 但是當我進行涉及操縱數組的計算時,有幾個數字變為0.0000,而不是最初的數字。 有人可以幫忙嗎?

#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是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);

這將通過myArray[20]設置myArray[0] myArray[20] 不允許訪問myArray[20] ,因為那是20個元素的數組的第21個元素。

請注意,編譯器並不總是足夠好為您檢測到此問題。 Visual C ++通過使程序崩潰(不管您相信與不相信)都可以幫到您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM