2012-10-03 78 views
-4

我寫了一個C++程序,實際上它是一個遊戲。 我收到此錯誤:關於堆腐敗的錯誤

Windows has triggered a breakpoint in bla bla...

有人可以幫我嗎?
這是代碼,但行AAA出現錯誤:

void r_motions(char **map,int size) 
{ 
    int parameter_i,parameter_j,player_i,player_j; 
    int *r_location_i = new int[1],*r_location_j = new int[1]; 
    player_finder(map,size,player_i,player_j); 
    int r_num = robots_finder(map,size,r_location_i,r_location_j); 
    for(int i=1;i<=r_num;i++) 
    { 
     parameter_i =0; 
     parameter_j =0; 
     if(r_location_i[i]>player_i) parameter_i = -1; 
     if(r_location_i[i]<player_i) parameter_i = 1; 
     if(r_location_j[i]>player_j) parameter_j = -1; 
     if(r_location_j[i]<player_j) parameter_j = 1; 
     map[r_location_i[i]][r_location_j[i]] = '.'; 
     r_location_i[i] = r_location_i[i]+parameter_i; 
     r_location_j[i] = r_location_j[i]+parameter_j; 
    } 
    for(int i=1;i<=r_num;i++) 
    { 
     switch (map[r_location_i[i]][r_location_j[i]]) 
     { 
     case '.': 
      map[r_location_i[i]][r_location_j[i]] = '+'; 
      break; 
     case '@': 
      map[r_location_i[i]][r_location_j[i]] = '+'; 
      print_map(map,size); 
      cout << "Robots win ." << endl; 
      sleep(1000); 
      exit(1); 
      break; 
     case '+': 
      map[r_location_i[i]][r_location_j[i]] = '*'; 
      break; 
     case '*': 
      map[r_location_i[i]][r_location_j[i]] = '*'; 
      break; 
     default: cout << "what r u doin' ??"; 
      break; 
     } 
    } 
} 
+0

所以,你不會告訴我們完整的錯誤,或告訴我們它實際上發生了哪一行,而且你還沒有嘗試簡化你的代碼來找出哪個功能導致問題? – Useless

+0

你確定'map'指向一個有效的數組嗎?乍一看,我會說你正在用指針做一些可怕的事情,爲了安全起見,應該把它們包裝在類中,並且/或者使用STL容器。 – Beta

回答

3

沒事,只是個開始,看看這個:

int *r_location_i = new int[1], ...; 
... 
for(int i=1;i<=r_num;i++) 
    { 
    parameter_i =0; 
    ... 
    if(r_location_i[i]>player_i) parameter_i = -1; // reading outside the array 
    ... 
    r_location_i[i] = r_location_i[i]+parameter_i; // writing outside the array 
    ... 
    } 

回去研究陣列。 直到您確切知道上面的代碼出了什麼問題之前,請勿觸摸另一個指針