繁体   English   中英

“检测到堆栈粉碎”错误

[英]'stack smashing detected' error

我有一个作业,我要使用多维数组,并使用“ 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;
}

提前非常感谢您。 (附言:我只需要自己做一个硬件,便是一个说明和一些技巧。)

大小为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个位置插入值时,您试图将值插入为数组分配的内存之外。

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

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

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

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 ++入门级教科书中进行了描述。 阅读入门材料是一个好主意,而不是明显地假设您知道(例如,通过与另一种工作原理不同的编程语言进行类比)。

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

/*
 * 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
    }
}

暂无
暂无

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

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