c++ - Code::Blocks 在执行嵌套循环时返回 -10737741819 (0xC0000005)
问题描述
我一直在尝试使用嵌套循环将整数插入以下格式的二维数组:
1, 2, 3, 4, ,5 ,6 ,7 ,8 ,9 ,10
2, 4 ,6 ,8 ,10, 12, 14, 16, 18, 20
3、6、9、12、15、18、21、24、27、30
...
...
10, 20, 30,40, 50, 60, 70, 80, 90, 100
我使用以下代码生成结果:
#include <iostream>
using namespace std;
int main() {
int table[10][10];
for(int i = 1; i <= 10; i++) {
for(int j = 1; j <= 10; j++) {
table[i][j] = (j * i);
cout << table[i][j] << "\t"<< flush;
}
cout << endl;
}
return 0;
}
代码运行成功,但在执行过程中出错:
我目前正在使用 Code::Blocks + GNU GCC 编译器。我该如何解决这个问题?是因为我的代码吗?还是编译器?
解决方案
您应该从 0(包括)开始迭代到 10(不包括):
[在线试用]
#include <iostream>
using namespace std;
int main() {
int table[10][10];
for(int i = 0; i < 10; ++i) {
for(int j = 0; j < 10; ++j) {
table[i][j] = ((j+1) * (i+1));
cout << table[i][j] << "\t"<< flush;
}
cout << endl;
}
return 0;
}
注意:如果你使用 C++17,你可以使用std::size来避免多次硬编码数组大小。(或者,您可以使用一些编译器特定的宏。)
推荐阅读
- graphviz - 如何设置圆角节点样式的半径?
- java - 无法解析符号 QueryParser Lucene 7.5
- python - Class 和 Dict 为什么不合并?
- python - python中的winsound - 显示器上的低延迟,笔记本电脑上的高延迟为什么?
- function-pointers - Nim:具有副作用的别名程序
- apache - 具有两个或多个字段的 AEM OSGi 配置多重属性
- excel - 使用 Acumatica 的导出方法为 Excel 单元格填充颜色
- java - 如何在Java中连接两个列表中的单词
- elixir - 在我的 elixir/phoenix 项目中执行操作后如何在 aws cloudfront 中创建失效
- java - 如何在每个 @Test 之前运行 @BeforeClass 方法