2011-05-03 181 views
0

我的代碼atm有問題,我不知道是什麼問題,因爲我在startFire函數中操作數組時遇到狀態訪問衝突異常。我相當肯定沒有任何數組超出界限錯誤,但也許我沒有看到它:/或它沒有看到的指針/引用錯誤。希望另一雙眼睛可以看看。二維矩陣STATUS_ACCESS_VIOLATION錯誤

這裏是我的代碼:

#include <cstdlib> 
#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <string> 

using namespace std; 

string cfile, ccommand; 
int cnum1,cnum2,cnum3,cnum4,cx; 
//bool fired = false; 
bool flag = true; 
const double h = .2; 

/* 
* 
*/ 

void printMatrix(double **x, int n) 
{ 
    int size = n; 
    for(int i=0; i<size; i++) 
    { 
     for(int j=0; j<size; j++) 
     { 
      std:: cout << x[i][j] << " " ; 
     } 
     std:: cout << std::endl; 
    } 


} 


void readFile(string file,double **x, int n) 
{ 
    const char* filename = file.c_str(); 
    std::ifstream inFile(filename); 

    int size = n; 

    if(!inFile) 
    { 
     cout<<endl<<"Failed to open file " << filename; 
    } 

    for(int i=0; i<size; i++) 
    { 
     for(int j=0; j<size; j++) 
     { 
      inFile >> x[i][j]; 
     } 
    } 
} 

void startFire(double **x, int n, int it) 
{ 
    int size = n; 
    int iteration = it; 

    /* Initialize 2D Array */ 
    double **matrix; 

    matrix = new double*[n]; 
    for(int i = 0; i<n; i++) 
     matrix[i] = new double[n]; 

    for(int j=0; j<n; j++) 
     for(int i=0; i<n;i++) 
      matrix[i][j] = 0; 

    for() 

    for(int iter=1; iter<=iteration; iter++) 
    { 
     cout<<"Iteration #: "<<iter<<endl; 
     for(int i=1; i<size; ++i) 
     { 
      for(int j=1; j<size; ++j) 
      { 
       matrix[i][j] = .25 *(x[i-1][j]+x[i+1][j]+x[i][j-1]+x[i][j+1]); 

      } 
     } 
     printMatrix(matrix,size); 
    } 
} 



void GetCommandLineArguments(int argc, char **argv,string &file, int &n, int &k, int &m, int &i) 
{ 
    if(argc = 6) 
    { 
     cfile = argv[1]; 
     cnum1 = atoi(argv[2]); 
     cnum2 = atoi(argv[3]); 
     cnum3 = atoi(argv[4]); 
     cnum4 = atoi(argv[5]); 
    } 
    file = cfile; 
    n = cnum1; 
    k = cnum2; 
    m = cnum3; 
    i = cnum4; 

} 



int main(int argc, char** argv) { 

    int k; //Factor of n 
    int m; //Innner matrix size 
    int i; //Iteration 
    int n; //Matrix Size 
    string file; 
    int input; 


    /*Takes in the initial cmd line values*/ 
    GetCommandLineArguments(argc, argv, file, n, k, m, i); 


    /* Initialize 2D Array */ 
    double **matrix; 

    matrix = new double*[n]; 
    for(int i = 0; i<n; i++) 
     matrix[i] = new double[n]; 

    for(int j=0; j<n; j++) 
     for(int i=0; i<n;i++) 
      matrix[i][j] = 0; 


    /*Reads a file with numbers to make matrix*/ 
    readFile(file,matrix,n); 

    /*Prints array displaying a matrix*/ 
    printMatrix(matrix,n); 



    /*To call the fire command that will access the middle matrix*/ 
    while(flag != false) 
    { 
     cout<<endl 
     <<"MENU:\n" 
     <<"1 - Start Fire.\n" 
     <<"2 - Stop Fire.\n" 
     <<"3 - QUIT.\n" 
     <<" Enter your choice and press return: "; 
     cin >> input; 

     switch(input) 
     { 
      case 1: 
       cout<<"Starting fire.\n"; 
       startFire(matrix, n,i); 
       //fired = true; 

       break; 
      case 2: 
       cout<<"Stop fire.\n"; 
       //fired = false; 
       break; 
      case 3: 
       cout<<"Ending program.\n"; 
       flag = false; 
       break; 
      default: 
       cout<<"Not a valid choice.\n"; 
       cout<<"Choose again.\n"; 
       cin>>input; 
       break; 
     } 
    } 




    return 0; 
} 

我的文件輸入稱爲sample.input發生在下列號碼

20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
200.0 
200.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
200.0 
200.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 
20.0 

下面是我通過CMD線得到的錯誤。

http://i53.tinypic.com/2qu4078.jpg

回答

3

您所訪問的分配的數組的大小之外:

matrix[i][j] = .25 *(x[i-1][j]+x[i+1][j]+x[i][j-1]+x[i][j+1]); 

對於內核操作,你真的應該分配矩陣各一個邊緣較大(+2總),要麼清零或複製邊緣值(取決於您的矩陣操作)。您需要調整您的指數以考慮此偏移量。

(或者,您也可以夾緊你的指數永遠是矩陣內。)

+0

好吧,這是真的,它的工作感謝信息:) – Novazero 2011-05-03 04:21:00

2
for(int i=1; i<size; ++i) 
{ 
    for(int j=1; j<size; ++j) 
    { 
     matrix[i][j] = .25 *(x[i-1][j]+x[i+1][j]+x[i][j-1]+x[i][j+1]); 

    } 
} 

通知在最後一次迭代即I =大小-1,J部分

x[i][j+1] 

=大小1,將達到矩陣外