2016-04-12 22 views
-3

我已經用盡了所有我知道的調試,並決定在這裏尋求幫助。這是爲了模擬表格3和表4的FIFO算法分頁的學校作業,試圖監視我們是否可以觀察Beladys異常發生。非常奇怪的問題嵌套循環在C中,數據不一致

基本上我有我的主要如下代碼:

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 

爲缺乏間隔道歉。我能想到的唯一的事情是我重新聲明我在我調用的某些函數中,但這是本地範圍,所以它應該不重要。

代碼永遠不會終止,因爲p永遠不會達到1000.有誰知道這裏發生了什麼?

+3

訪問'fault [4]'會導致未定義的行爲 –

+0

除了@sunqingyao說的什麼,我沒有什麼可以告訴你什麼是錯的。 – ddz

+0

請顯示[最小的完整和可驗證的示例](https://stackoverflow.com/help/mcve)。除了已經確定的越界數組訪問之外,您可能還有其他未引用的函數中的錯誤(例如'initializeTables')導致了未定義的行爲。 – kaylum

回答

0

arr []有多大?

其中一個函數(未在此處粘貼)可能會在其邊界外寫入數據,導致變量p被覆蓋。