Ich habe ein Array:Schreiben auf Array-Element [i] [0] [1] auch überschreibt [i] [1] [0] und vice versa
typedef short int long_graph[1000][1000][1];
I erklären es:
long_graph paf_graph;
ich habe eine Funktion:
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
ich das Problem hier erkannt:
Um dieses seltsame Phänomen zu isolieren Ich habe 3 zusätzliche Zeilen wie in dem größeren Codeblock gesehen:
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
Das seltsame ist das Array selbst ist [1000] [1000] [1] von short int und für andere Koordinaten funktioniert es gut. Ich habe versucht, es kleiner [100] [100] [1], aber das Problem ist das gleiche, die Werte, die übergeben werden, sind weniger als 100 so gut innerhalb kurzer Int.
Das Array wird als Referenz an die Funktion übergeben und ich benutze es danach im Hauptblock und alle Werte sind wie erwartet gesetzt außer [i] [0] [1] und [i] [1] [0] .
Leider kann ich nicht den gesamten Code, da es sehr groß ist, aber ich habe mit viel Protokollierung doppelt überprüft, dass das Problem zu dem oben beschriebenen Teil isoliert ist.
Wenn jemand eine Idee hat, warum dieses seltsame Verhalten passiert, bitte helfen.
Ich habe auch Codeblocks getaggt, da das ist, was ich benutze, ist es vielleicht ein bekannter Fehler mit dem Mingw Compiler?
Wie auch immer, danke im Voraus für jeden Hinweis.
Vielen Dank! Ich kann nicht glauben, dass ich auf einen solchen Anfängerfehler hereingefallen bin. – user3515319