這是給出堆棧溢出的代碼,它只發生了大約一半的時間,我不知道它爲什麼這樣做。從我看到它只發生在Coms(TopCom等)的大量數字中,因此大約5+然後堆棧溢出。爲什麼我的C#代碼導致堆棧溢出
public bool getConnected(int d) {
if (topCom.connection != null) {
if (d != topCom.connection.id) {
if (topCom.connection.id == 0) {
return true;
} else if (topCom.connection.connected == true) {
if (Development.instance.currentDos.buttons[topCom.connection.id].getConnected(id)) {
return true;
}
}
}
}
if (leftCom.connection != null) {
if (d != leftCom.connection.id) {
if (leftCom.connection.id == 0) {
return true;
} else if (leftCom.connection.connected == true) {
if (Development.instance.currentDos.buttons[leftCom.connection.id].getConnected(id)) {
return true;
}
}
}
}
if (rightCom.connection != null) {
if (d != rightCom.connection.id) {
if (rightCom.connection.id == 0) {
return true;
} else if (rightCom.connection.connected == true) {
if (Development.instance.currentDos.buttons[rightCom.connection.id].getConnected(id)) {
return true;
}
}
}
}
if (botCom.connection != null) {
if (d != botCom.connection.id) {
if (botCom.connection.id == 0) {
return true;
} else if (botCom.connection.connected == true) {
if (Development.instance.currentDos.buttons[botCom.connection.id].getConnected(id)) {
return true;
}
}
}
}
return false;
}
你試圖調試代碼。?我們甚至不知道你的結構的定義 - 我們知道的是,代碼可能以無限循環結束(當前實例ce和其中一個'topCom'形式*一個循環*) - 我不可能給出一個合理的解決方案,而不是將HashSet與*已經訪問的*節點一起傳遞,以便可以檢測循環 – Carsten 2014-10-27 05:08:16
我一直在使用堆棧跟蹤和調試我剛剛嘗試清理顯示代碼的最後四個小時的聲明。 – Dillyo09 2014-10-27 05:15:30
然後你必須重新思考你的算法(我們一無所知) - 你**必須找到某種方式來擺脫圖中的週期 - 我告訴過你一種可能性(使用訪問節點的HashSet) – Carsten 2014-10-27 05:32:13