2011-05-03 15 views
0

내 코드 atm에 문제가 있으며 시작 기능에서 배열을 조작 할 때 상태 액세스 위반 예외가 발생하므로 문제가 무엇인지 확실하지 않습니다. 임 확실히 꽤 거기에 무슨 일이 일어나는 배열 오류가 아니지만 어쩌면 내가 그것을 볼 수 없습니다 :/또는 자사의 포인터/참조 오류가 보이지 않는. 다른 한 쌍의 눈이 보일 수 있기를 바랍니다. 여기에 내가 cmd를 라인을 통해 얻을 오류입니다2D Matrix 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 

에서 :

여기 내 코드입니다.

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

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]); 

    } 
} 

통지 부분

x[i][j+1] 

맨 마지막 반복, 즉 I = 크기-1, J에서 = 매트릭스 외부에 닿는 크기 1