我有一個數組:寫入數組元素[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
我發現這裏的問題:
//paf_graph[n][0][1] = i; //this was not commented originally
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; //when k is 0 e.g. [n][1][0] it also overwrites [n][0][1] with the value
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";
}
爲了分離這種奇怪現象我把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]和對於任何其他座標,它工作正常。 我試過讓它變小[100] [100] [1],但問題是一樣的,在short int中傳遞的值低於100。
該數組作爲函數的參考傳遞,我在主塊中使用它,除[i] [0] [1]和[i] [1] [0] 。
不幸的是我不能發佈所有的代碼,因爲它非常大,但我已經用大量的日誌記錄來檢查問題是否與上面描述的部分分離。
如果有人有任何想法爲什麼這種奇怪的行爲發生請幫助。
我也標記了codeblocks,因爲這是我使用的,也許這是一個與mingw編譯器已知的錯誤?
無論如何,預先感謝您的任何建議。
謝謝!我不敢相信我會因爲這樣的新秀錯誤而墮落。 – user3515319