Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ ';检测到堆栈崩溃';错误_C++_Arrays_Multidimensional Array - Fatal编程技术网

C++ ';检测到堆栈崩溃';错误

C++ ';检测到堆栈崩溃';错误,c++,arrays,multidimensional-array,C++,Arrays,Multidimensional Array,我有一个任务,我要使用一个多维数组,并使用“iomanip”库正确输出从1到12的乘法表(有点像学校笔记本背面的乘法表)。虽然我得到了所需的输出,但正好在正确的数组索引下,我得到了这个错误 ***stack smashing error detected*** : <unknown> terminated ***检测到堆栈粉碎错误***:终止 我的代码还没有完成,这只是我在CS的第二个学期。在下面的代码中,我首先测试一种算法,用于在主函数内创建表,然后将其移植到独立函数,因为我们

我有一个任务,我要使用一个多维数组,并使用“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;
}
#包括
#包括
使用名称空间std;
int main()
{
int multTable[12][12];
int测试仪;

对于(inti=1;iC/C++数组,从索引0开始

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


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

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


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

大小为12的数组将包含从索引0到索引11的元素。您应该执行以下操作:

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

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

大小为12的数组将包含从索引0到索引11的元素。您应该执行以下操作:

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

较早,您从1开始到12,这意味着当您试图在第十二位插入一个值时,您试图将值插入到为数组分配的内存之外。C++中的“/P>< P>数组索引是零的(第一个元素有索引<代码> 0代码>),而不是一个基于.< /P> <>访问“代码> MultTAB[< ] />代码或(通过它)<代码>多表[12 ] [j] < /C>在代码中的所有内循环中给出了未定义的行为。在C++标准中,“未定义”的含义实质上是“标准不定义结果是什么”。 实际上,就您的程序而言,使用无效数组索引写入值会覆盖一些不存在的内存。堆栈破坏(意味着您的程序在启动时破坏了操作系统分配给它的堆栈)是一种可能的影响,但不是唯一的影响

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

for(int i = 0; i < 12; i++) 
{
    for(int j = 0; j < 12; j++)
    { 
        multTable[i][j] = (i + 1) * (j + 1);
    }
}
在现代C++中,通常使用标准容器而不是原始数组是更好的选择。我将把它作为一个练习。


上述所有内容都是在C++的入门教材中介绍的。阅读介绍性的材料是一个好主意,而不是明显假设你知道(例如,用另一种编程语言进行不同的编程)。C++中的数组索引是零的(第一元素有索引<代码> 0代码>代码),而不是基于一个。 <>访问“代码> MultTAB[< ] />代码或(通过它)<代码>多表[12 ] [j] < /C>在代码中的所有内循环中给出了未定义的行为。在C++标准中,“未定义”的含义实质上是“标准不定义结果是什么”。 实际上,就您的程序而言,使用无效数组索引写入值会覆盖一些不存在的内存。堆栈破坏(意味着您的程序在启动时破坏了操作系统分配给它的堆栈)是一种可能的影响,但不是唯一的影响

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

for(int i = 0; i < 12; i++) 
{
    for(int j = 0; j < 12; j++)
    { 
        multTable[i][j] = (i + 1) * (j + 1);
    }
}
在现代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
    }
}
/*
*该程序在乘法表中创建一个大小为12X12的数组
*从1到12,使用2个函数,其中一个函数名为multTable(),不接受
*参数和另一个函数是displayTable(),它接受一个数组
*作为参数,并使用iomanip以矩阵格式输出数组
*图书馆。
*/
#包括
#包括
使用名称空间std;
静态常数int COL=12;
静态常数int行=12;
void multTable();//void类型的multTable()函数原型
void displayTable(int arr1[][ROW]);//displayTable()函数原型
int main()
{

cout我已经考虑了所指出的内容并纠正了我的错误。这是最后的代码,它的工作原理与我希望的一样:

/*
 * 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
    }
}
/*
*该程序在乘法表中创建一个大小为12X12的数组
*从1到12,使用2个函数,其中一个函数名为multTable(),不接受
*参数和另一个函数是displayTable(),它接受一个数组
*作为参数,并使用iomanip以矩阵格式输出数组
*图书馆。
*/
#包括
#包括
使用名称空间std;
静态常数整数列