“堆叠粉碎检测到的”错误
'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;
}
非常感谢您。(PS:一个解释和一些技巧是唯一需要的事情,我想自己做我的HW。(
大小12的数组将具有从索引0到索引11的元素。您应该这样做:
for(int i = 0; i < 12; i++)
{
for(int j = 0; j < 12; j++)
{
multTable[i][j] = i * j;
}
}
之前,您从1到12开始,这意味着当您尝试在第十二位置插入一个值时,您正在尝试在分配给数组的内存之外插入值。
c/c 数组从索引零开始。
相反,如果迭代为1到12,则必须从零到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:nn";
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
}
}
- 错误:在尝试检测 std::cout 是否<< t 时,功能强制转换为数组类型;有效
- 如何从标准输入中检测格式化输入错误?
- 程序停止运行,但未检测到错误
- JNI 在应用程序中检测到错误:在为 innerclass 调用 NewObject 时使用了无效的 jobject
- 在C++中检测到堆损坏错误
- 未知C++错误:致命错误:glibc检测到无效的stdio句柄
- Clang 无法检测到初始化并给出错误
- 如何检测内存分配/空闲错误的对象?
- 想要构建一些必要的库来检测功能.出现一些错误
- Android (ART) 崩溃并显示错误 JNI 在应用程序中检测到错误:jstring 是无效的本地引用
- 保护类内存阵列以检测分段错误
- 舍入错误检测
- 从emxArray_uint8_T检测到错误:.text+0xc1):未定义对"emxInit_uint8_T1(emxArray_uint8_T**, int)"的引用?
- 排序程序的错误(检测到堆损坏)
- LLVM 的位码错误检测函数参数
- 类对象的可变级别内存错误检测
- 错误: *** 检测到 glibc *** w5:双重释放或损坏(快速顶部):
- 错误检测Visual Studio 2010
- 同名功能中的错误检测
- GetModuleFileName防病毒错误检测??获取当前进程PATH