2016-04-12 13 views
-3

私はここで知っているすべてのデバッグを使い果たし、ここで助けを求めることにしました。これは、Beladysの異常が発生しているかどうかを監視するために、テーブルサイズ3と4のFIFOアルゴリズムのページングをシミュレートするための学校の割り当て用です。Cでネストされたforループで非常に奇妙な問題、データが矛盾しています

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; 

私は、次のグローバル宣言を持っています:

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 

int arr[101], count; 

私の出力はいえ、このようになります

は基本的に私は私のメインの中に次のコードを持っていますスペーシングがないことをお詫び申し上げます。私が考えることができる唯一のことは、私が呼び出す関数のいくつかで私が再宣言することですが、それはローカルスコープなので重要ではありません。

pは決して1000に達しないので、コードは決して終了しません。何が起こっているのか誰にも分かりますか?

+3

'faults [4]'にアクセスすると未定義の動作が発生します –

+0

@sunqingyaoが言ったことを除いて何が間違っているのか分かりません。 – ddz

+0

[最小限の完全で検証可能な例](https://stackoverflow.com/help/mcve)を表示してください。すでに定義されている範囲外の配列アクセスとは別に、未定義の動作を引き起こしている(例えば 'initializeTables'などの)表示されていない関数に他のバグがあるかもしれません。 – kaylum

答えて

0

arr []の大きさはどれくらいですか?

関数の1つ(ここには貼り付けられていません)は、おそらく変数pを上書きする原因となる可能性があります。

関連する問題