我有一个作业,我将使用多维数组并使用" iomanip"库从1到12正确输出乘法表(有点像学校笔记本如何在后面)。虽然我得到了所需的输出,正确的数组索引下,我收到此错误
***stack smashing error detected*** : <unknown> terminated
我的代码完全没有完成,这只是我在CS的第二个学期。在下面的代码中,我首先测试一个算法,用于在将函数移植到独立函数之前在main函数中创建表,因为我们的教授想要实现模块化或函数式编程。它如下:
#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:解释和一些提示是唯一需要的东西,我想自己做我的硬件。)
答案 0 :(得分:2)
大小为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个位置插入值时,您试图将值插入为数组分配的内存之外。
答案 1 :(得分:1)
C / C ++数组从索引零开始。
相反,如果从1迭代到12,则必须从0迭代到11。
写入multTable[12][12]
将写入函数堆栈空间之外的内存,从而破坏它。这是“堆栈粉碎”。
答案 2 :(得分:0)
C ++中的数组索引是基于零的(第一个元素具有索引0
),而不是基于一个。
在代码中内循环的所有迭代中访问multTable[12]
或(通过它)multiTable[12][j]
会产生未定义的行为。在C ++标准中,“undefined”的含义基本上是“标准没有定义结果会发生什么”。
实际上,就程序而言,使用无效数组索引写入值会覆盖一些不存在的内存。堆栈粉碎(意味着你的程序在操作系统启动时破坏了分配给它的堆栈)是一种可能的影响 - 但不是唯一的。
要更正问题,请更改循环以将索引减少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 ++入门教科书中有所描述。阅读介绍性材料是一个好主意,而不是明显地假设你知道(例如,通过类比另一种编程语言的工作方式不同)。
答案 3 :(得分:0)
我已经考虑到了指出的内容并纠正了我的错误。这是最终的代码,它正如我所希望的那样工作:
/*
* 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
}
}