0
我試圖驗證指針數組中的,所以我沒有造成任何內存錯誤,空指針類
而且這種方法:
for(int i=0;i<array_size;i++) {
if (array[i]!=NULL)
array[i]->stuff();
}
}
過去工作過。
現在,我必須做同樣的事情,除了基於對象變量來完成所有事情。
我的新方法是:
Direct2Entity* nextset[MAX_ENTS]; // ents[MAX_ENTS] is also a Direct2Entity* array
for(int i=0;i<MAX_ENTS;i++) {
nextset[i]=NULL; // note that ents[] is also flushed before this
}
int nextsetid=0;
int maxn;
bool stillnull;
while(true) { // infinite sorting loop
maxn=-1;
stillnull=true;
for(int i=0;i<next_put;i++) {
if (ents[i]!=NULL) {
stillnull=false;
if (ents[i]->depth<0) { // make sure no infinite loops occur with negative depth
ents[i]->depth=0;
}
if (ents[i]->depth>maxn) {
nextset[nextsetid++]=ents[i];
ents[i]=NULL; // make NULL to further loop
}
}
}
if (stillnull) break;
}
for(int i=0;i<next_put;i++) {
if (nextset[i]!=NULL) {
ents[i]=nextset[i]; // copy nextset[] to ents[]
}
}
for(int i=0;i<next_put;i++) {
if (ents[i]!=NULL) {
if (ents[i]->getroom()==current_room) {
ents[i]->draw(this); // ents[i] is still NULL... ?
}
}
}
在過去的for循環,經濟需求[I]被明確檢查,以確保它不會被取消引用空指針。然而C++通過它並調用函數。在各種隨機地方都有各種運行時錯誤,但我幾乎可以肯定這是來自這裏的未定義行爲。
使用調試器瀏覽您的程序。它會告訴你發生錯誤的地方。您將能夠在任何時候檢查變量並將這些值與您的預期進行比較。 – 2013-02-25 04:55:57
經過大約1000次f11和30個左右的彙編語言文件的處理後,我修復了重新排序算法的錯誤。你有沒有任何提示,只保留在你的文件的步入視圖..? – object 2013-02-25 05:13:46
@metredigm:在調試器中查找*斷點*。這允許執行直到遇到斷點。一些調試器允許代碼在* N *遇到斷點後停止(查找觸發器)。 – 2013-02-25 06:28:45