【问题标题】:Very Odd issue with nested for loops in C, data is inconsistentC中嵌套for循环的非常奇怪的问题,数据不一致
【发布时间】:2016-08-03 18:47:25
【问题描述】:

我已经用尽了我在这里知道的所有调试,并决定在这里寻求帮助。这是一个学校作业,旨在模拟表大小为 3 和 4 的 FIFO 算法的分页,以尝试监控我们是否可以观察到发生的 Beladys 异常。

基本上我的 main 中有以下代码:

int i;
srand(time(NULL));
int faults[2], threeTable[3], fourTable[4];
int p;
    int beladyOccured = 0;
    int beladyNot = 0;
    count = 20;
    for(p = 0; p < 1000; p++)
    {
        for(i = 0; i < 20; i++)
        {
            arr[i] = rand() % 5;
        }
        initializeTables(threeTable, 3);
        faults[0] = fifo(threeTable, 3);
        initializeTables(fourTable, 4);
        faults[1] = fifo(fourTable, 4);
        if (detectBelady(faults[1], faults[0]) == 1)
        {
            beladyOccured++;
            printf("Belady's Anomaly occured, p is %d, i is %d, number of faults for three is %d, number of faults for four is %d, belady occurances is %d, and non-occurances is %d", p, i, faults[0], faults[4], beladyOccured, beladyNot);
        }
        else
        {
            beladyNot++;
            printf("Belady's Anomaly did not occur, p is %d, i is %d, number of faults for three is %d, number of faults for four is %d, belady occurances is %d, and non-occurances is %d \n", p, i, faults[0], faults[4], beladyOccured, beladyNot);

        }
    }

    printf("Using frame sizes 3 and 4, beladys algorithm occured %d times and did not occur %d times \n", beladyOccured, beladyNot);
return 0;

我有以下全局声明:

int arr[101], count;

我的输出看起来像这样:

Belady's Anomaly did not occur, p is 3, i is 0, number of faults for three is 9, number of faults for four is 1, belady occurances is 1, and non-occurances is 12
Belady's Anomaly did not occur, p is 4, i is 4, number of faults for three is 10, number of faults for four is 1, belady occurances is 1, and non-occurances is 13
Belady's Anomaly did not occur, p is 3, i is 0, number of faults for three is 10, number of faults for four is 1, belady occurances is 1, and non-occurances is 14
Belady's Anomaly did not occur, p is 4, i is 0, number of faults for three is 10, number of faults for four is 1, belady occurances is 1, and non-occurances is 15
Belady's Anomaly did not occur, p is 4, i is 3, number of faults for three is 12, number of faults for four is 4, belady occurances is 4, and non-occurances is 4
Belady's Anomaly did not occur, p is 4, i is 4, number of faults for three is 10, number of faults for four is 4, belady occurances is 4, and non-occurances is 5
Belady's Anomaly did not occur, p is 5, i is 1, number of faults for three is 9, number of faults for four is 4, belady occurances is 4, and non-occurances is 6
Belady's Anomaly did not occur, p is 6, i is 4, number of faults for three is 9, number of faults for four is 4, belady occurances is 4, and non-occurances is 7
Belady's Anomaly did not occur, p is 1, i is 1, number of faults for three is 10, number of faults for four is 4, belady occurances is 4, and non-occurances is 8
Belady's Anomaly did not occur, p is 0, i is 3, number of faults for three is 12, number of faults for four is 3, belady occurances is 3, and non-occurances is 4
Belady's Anomaly did not occur, p is 1, i is 20, number of faults for three is 8, number of faults for four is 3, belady occurances is 3, and non-occurances is 5
Belady's Anomaly did not occur, p is 2, i is 20, number of faults for three is 8, number of faults for four is 3, belady occurances is 3, and non-occurances is 6
Belady's Anomaly did not occur, p is 3, i is 4, number of faults for three is 9, number of faults for four is 3, belady occurances is 3, and non-occurances is 7
Belady's Anomaly did not occur, p is 4, i is 20, number of faults for three is 7, number of faults for four is 3, belady occurances is 3, and non-occurances is 8
Belady's Anomaly did not occur, p is 5, i is 20, number of faults for three is 8, number of faults for four is 3, belady occurances is 3, and non-occurances is 9
Belady's Anomaly did not occur, p is 2, i is 0, number of faults for three is 10, number of faults for four is 3, belady occurances is 3, and non-occurances is 10
Belady's Anomaly did not occur, p is 3, i is 3, number of faults for three is 13, number of faults for four is 0, belady occurances is 0, and non-occurances is 1
Belady's Anomaly did not occur, p is 4, i is 0, number of faults for three is 11, number of faults for four is 0, belady occurances is 0, and non-occurances is 2
Belady's Anomaly did not occur, p is 5, i is 0, number of faults for three is 9, number of faults for four is 0, belady occurances is 0, and non-occurances is 3
Belady's Anomaly did not occur, p is 4, i is 4, number of faults for three is 10, number of faults for four is 0, belady occurances is 0, and non-occurances is 4
Belady's Anomaly did not occur, p is 5, i is 20, number of faults for three is 7, number of faults for four is 0, belady occurances is 0, and non-occurances is 5
Belady's Anomaly did not occur, p is 6, i is 20, number of faults for three is 7, number of faults for four is 0, belady occurances is 0, and non-occurances is 6
Belady's Anomaly did not occur, p is 7, i is 20, number of faults for three is 8, number of faults for four is 0, belady occurances is 0, and non-occurances is 7
Belady's Anomaly did not occur, p is 8, i is 20, number of faults for three is 7, number of faults for four is 0, belady occurances is 0, and non-occurances is 8
Belady's Anomaly did not occur, p is 9, i is 20, number of faults for three is 8, number of faults for four is 0, belady occurances is 0, and non-occurances is 9
Belady's Anomaly did not occur, p is 10, i is 20, number of faults for three is 8, number of faults for four is 0, belady occurances is 0, and non-occurances is 10
Belady's Anomaly did not occur, p is 0, i is 1, number of faults for three is 11, number of faults for four is 1, belady occurances is 1, and non-occurances is 11
Belady's Anomaly did not occur, p is 1, i is 0, number of faults for three is 9, number of faults for four is 1, belady occurances is 1, and non-occurances is 12
Belady's Anomaly did not occur, p is 1, i is 2, number of faults for three is 14, number of faults for four is 1, belady occurances is 1, and non-occurances is 2
Belady's Anomaly did not occur, p is 0, i is 0, number of faults for three is 10, number of faults for four is 1, belady occurances is 1, and non-occurances is 3
Belady's Anomaly did not occur, p is 1, i is 20, number of faults for three is 8, number of faults for four is 1, belady occurances is 1, and non-occurances is 4
Belady's Anomaly did not occur, p is 2, i is 3, number of faults for three is 9, number of faults for four is 1, belady occurances is 1, and non-occurances is 5
Belady's Anomaly did not occur, p is 3, i is 1, number of faults for three is 9, number of faults for four is 1, belady occurances is 1, and non-occurances is 6
Belady's Anomaly did not occur, p is 4, i is 20, number of faults for three is 8, number of faults for four is 1, belady occurances is 1, and non-occurances is 7

为缺少间距而道歉。我唯一能想到的是我在我调用的一些函数中重新声明了 i,但那是本地范围,所以它应该无关紧要。

代码永远不会终止,因为 p 永远不会达到 1000。有人知道这里发生了什么吗?

【问题讨论】:

  • 访问 faults[4] 会导致未定义的行为
  • 除了@sunqingyao 说的,我没有什么可以说的。
  • 请显示minimal complete and verifiable example。除了已经确定的越界数组访问之外,您可能在未显示的函数中存在其他错误(例如initializeTables),这些错误会导致未定义的行为。

标签: c for-loop random nested infinite-loop


【解决方案1】:

arr[] 有多大?

其中一个函数(此处未粘贴)可能会将数据写入其边界之外,从而导致变量 p 被覆盖。

【讨论】:

    猜你喜欢
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 2011-06-25
    • 2011-05-31
    • 1970-01-01
    相关资源
    最近更新 更多