首页 > 解决方案 > 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 编译器。我该如何解决这个问题?是因为我的代码吗?还是编译器?

标签: c++codeblocks

解决方案


您应该从 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来避免多次硬编码数组大小。(或者,您可以使用一些编译器特定的宏。)


推荐阅读