【问题标题】:'stack smashing detected' error“检测到堆栈粉碎”错误
【发布时间】:2018-02-08 06:18:13
【问题描述】:

我有一个任务,我要使用一个多维数组并使用“iomanip”库来正确输出从 1 到 12 的乘法表(有点像学校笔记本背面的乘法表)。虽然我在正确的数组索引下得到了所需的输出,但我得到了这个错误

***stack smashing error detected*** : <unknown> terminated

我的代码还没有完成,这只是我在 CS 的第二个学期。在下面的代码中,我首先测试了一种算法,用于在主函数中创建表,然后将其移植到独立函数中,因为我们的教授想要实现模块化或函数式编程。如下:

#include<iostream>
#include<iomanip>

using namespace std;

int main() 
{
    int multTable[12][12];
    int tester;

    for(int i = 1; i <= 12; i++)
    {
        for(int j = 1; j <= 12; j++)
        { 
            multTable[i][j] = i * j;
        }
    }

    tester = multTable[2][3];//this displays the correct number, 6
    cout << tester << endl;


    return 0;
}

提前非常感谢您。 (PS:解释和一些提示是唯一需要的东西,我想自己做我的硬件。)

【问题讨论】:

    标签: c++ arrays multidimensional-array


    【解决方案1】:

    大小为 12 的数组将包含从索引 0 到索引 11 的元素。您应该这样做:

    for(int i = 0; i < 12; i++)
    {
        for(int j = 0; j < 12; j++)
        { 
            multTable[i][j] = i * j;
        }
    }
    

    之前,您是从 1 开始到 12,这意味着当您尝试在第 12 位插入值时,您是在尝试将值插入到为数组分配的内存之外。

    【讨论】:

      【解决方案2】:

      C/C++ 数组从索引零开始。

      如果从 1 迭代到 12,则必须从 0 迭代到 11。

      写入multTable[12][12] 将写入函数堆栈空间之外的内存,从而破坏它。这是“堆栈粉碎”。

      【讨论】:

        【解决方案3】:

        C++ 中的数组索引是从零开始的(第一个元素的索引是 0),而不是从一开始的。

        在代码内循环的所有迭代中访问multTable[12] 或(通过它)multiTable[12][j] 会产生未定义的行为。在 C++ 标准中,“未定义”的含义本质上是“标准没有定义结果会发生什么”。

        实际上,就您的程序而言,使用无效的数组索引写入值会覆盖一些不存在的内存。堆栈粉碎(意味着您的程序在启动时已将操作系统分配给它的堆栈丢弃)是其中一种可能的影响 - 但不是唯一的影响。

        要纠正问题,请更改循环以将索引减少1。例如 - 更改两个循环中的开始和结束条件。

        for(int i = 0; i < 12; i++) 
        {
            for(int j = 0; j < 12; j++)
            { 
                multTable[i][j] = (i + 1) * (j + 1);
            }
        }
        

        注意循环体中的赋值也发生了变化。

        由于数组索引被移动,您还需要通过减少两个索引来更改访问值的方式。

        tester = multTable[1][2];//this displays the correct number, 6
        

        在现代 C++ 中,通常最好使用标准容器,而不是原始数组。我会把它留作练习。

        以上所有内容均在 C++ 入门教科书中进行了描述。阅读介绍性材料是个好主意,而不是假设您知道(例如,通过类比另一种工作方式不同的编程语言)。

        【讨论】:

          【解决方案4】:

          我已经考虑了所指出的内容并纠正了我的错误。这是最终代码,它可以按照我的意愿工作:

          /*
           * This program creates an array of size 12X12 two displpay the multiplication tables
           * from 1-12, uses 2 functions, one function called multTable() that takes in no
           * parameters and the other function is displayTable(), which takes in an array
           * as a parameter and outputs the array in matrix format using the iomanip
           * library.
           */
          #include<iostream>
          #include<iomanip>
          
          using namespace std;
          
          static const int COL = 12;
          static const int ROW = 12;
          
          
          void multTable();//multTable() function prototype of type void
          void displayTable(int arr1[][ROW]);//displayTable() function prototype
          
          
          
          int main() 
          {
              cout << "This program outputs the multiplication tables from 1-12:\n\n";
              multTable();//function call to multTable()
          
              return 0;
          }
          
          void multTable()//function definition for multTable, takes in no formal parameters
          {
              int table[COL][ROW];//declares an array of size COL X ROW
              int tester;
          
              for(int i = 0; i < ROW ; i++)//loop through each row
              {
                  for(int j = 0; j <  COL; j++)//loop though each element in each row
                  { 
                      table[i][j] = (i + 1) * (j + 1);//since arrays are 0 indexed, i and j must be incremented by 1, in order
                                                     //to achieve desired value of element
                  }
              }
              displayTable(table);//function call to displayTable() with table as actual parameter
          }
          
          void displayTable(int arr1[][COL])//function definition fror displayTable(), takes in
                                            //an array of type int as its formal parameter
          {
              for(int a = 0; a < 12; a++)//loop through each row
              {
                  for(int b = 0; b < 12; b++)//loop through each column
                  {
          
                      cout << left << setw(5) << arr1[a][b];//prints all contents of the ath row, left justified with
                                                            //with a width of 5
                  }
                  cout << endl;//starts the bth row on a new line per iteration
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-07-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-09-28
            • 1970-01-01
            相关资源
            最近更新 更多