2017-02-12 10 views
0

Iは、配列を有する:[I] [0] [1]も上書き[I] [1] [0]及びその逆

typedef short int long_graph[1000][1000][1]; 

私はそれを宣言する:

long_graph paf_graph; 

は私が機能を持っている:

:私はここでの問題を検出し
void construct_paf_graph (sf::Vector2i A,sf::Vector2i B, long_graph &paf_graph, long_list &paf_list, poly_list &poly, int &no_of_poly_on_map, list_of_indexes &XX ) 
{ 

short int n,i,j,k,e, t, off; // t is used to convert indices from individual  polies to the unified list 
bool path_clear; 
short int distance, rev_en_ind; // reverse engineer index 

n=0; //will be total number of points in paf_graph and paf_list 
t=0; 

std::fstream debug; 
debug.open("assets/debug.txt", std::ios::out | std::ios::trunc); 

for (i=1; i<=XX[0]; i+=1) 
{ 
     debug << XX[i] << "\n"; 
     for (j=1; j<= poly[XX[i]].point[0].x; j+=1) 
      { 
       n+=1; 
       paf_graph[n][0][0] = poly[XX[i]].graph[j][0][0]; 
       //paf_graph[n][0][1] = i; 
       debug << "P["<<n<<"] = " << paf_graph[n][0][1] << " \n"; 

       for (k=1; k<= poly[XX[i]].graph[j][0][0]; k+=1) 
        { 
          paf_graph[n][k][0] = poly[XX[i]].graph[j][k][0] + t; 
          paf_graph[n][k][1] = poly[XX[i]].graph[j][k][1]; 
          debug << " P["<<n<<"] = " << paf_graph[n][0][1] << " " << "k="<<k<< " c="<<poly[XX[i]].graph[j][k][0] + t<<" \n"; 
          if (k==1) debug << "\n\n PP = " << paf_graph[n][k][0] << " " << poly[XX[i]].graph[j][k][0] + t <<"\n"; 
        } 

       off = paf_graph[n][1][0]; 
       paf_graph[n][0][1] = i; 
       //paf_graph[n][1][0] = off; 

       paf_list[n].x = poly[XX[i]].point[j].x; 
       paf_list[n].y = poly[XX[i]].point[j].y; 

      } 
      debug << "\n"; 

t+= poly[XX[i]].point[0].x; 
} 

paf_graph[0][0][0] = n; 
paf_list[0].x = n; 
// function continues some more 

より大きなコードブロックに見られるように

210はI 3つの余分な行を入れて、この奇妙な現象を単離する:配列自体は

off = paf_graph[n][1][0]; 
paf_graph[n][0][1] = i; //this line also writes i to [n][1][0] 
//paf_graph[n][1][0] = off; //if I take the comment off this line it will 
          // write "off" to [n][0][1] as well 
          //so whatever I do they both have the same value 
          //and one is always wrong 

奇妙なことである[1000] [1000] [1]ショートINTおよびそれ以外の座標については正常に動作します。 私はそれを小さくしようとしましたが[100] [100] [1]問題は同じです、渡される値は100未満です。

配列は関数への参照として渡され、後でメインブロックで使用され、[i] [0] [1] [i] [1] [0] 。

残念ながら、コードは非常に大きいので、私はすべてのコードを投稿できませんが、問題が上記の部分に分離されていることを二重チェックしました。

この奇妙な現象が起こっている理由が分かっている人は、助けてください。

これは私がおそらくmingwコンパイラで知られているエラーですので、コードブロックにタグを付けましたか?

とにかく、アドバイスありがとうございました。

答えて

3

ユースケースを簡略化した後、あなたのバグは非常に明白になります。

サイズが配列の最後の次元のサイズと等しい単純な1次元配列を使用しましょう。代わりに:

typedef short int long_graph[1000][1000][1]; 

ちょうどそのサイズは最後の次元のサイズと同じである1次元配列を使用してみましょう:今すぐ

typedef short int tiny_graph[1]; 

を、あなたは、このようなものを持って宣言:

tiny_graph x; 

ここで、有効な配列インデックスは何ですか?

答えは非常に明白です。 1つだけ:x[0]。この配列は1つの値しか持たないので、配列のインデックスは0から始まります。

さて、あなたの配列に戻ってみましょう:

typedef short int long_graph[1000][1000][1]; 

との問題が声明を見てみましょう:

paf_graph[n][0][1] = i 

あなたのバグは今、非常に明白でなければなりません。 [0]は、この配列の最後の3番目の次元の最後の有効なインデックスであるため、paf_graph[n][0][1]はありません。そして、配列がポインタとポインタ演算に崩壊するため、次の次元の配列の[0]が解決されます。

+0

ありがとうございました!私はそのようなルーキーミスのために落ちたとは信じられません。 – user3515319

関連する問題