【发布时间】:2014-10-05 09:59:59
【问题描述】:
void gameListType::sortAscending() {
nodeType<gameType> *current;
nodeType<gameType> *next;
nodeType<gameType> *prev;
nodeType<gameType> *temp;
temp = first;
prev = first;
current = first;
next = current->link;
while ( current->link !=NULL){
if (current ==first){
if (current->info.getPrice() > next->info.getPrice()){
temp->info = current -> info;
temp->link = next ->link;
next -> link = temp;
current = temp;
delete temp;
}
}
else if (current !=first ){
if (current->info.getPrice() > next->info.getPrice()){
temp->info = current->info;
temp-> link = next ->link;
next->link = temp;
prev->link = next;
current = temp;
delete temp;
}
}
if(current == first){
prev = first;
}
else{
prev = prev->link;
}
current = current ->link;
next = next->link;
}
}
运行此代码会导致我的程序崩溃,有什么想法吗?这基本上是对 CUSTOM 链表中的项目进行排序。它显然只在循环完成一次后崩溃,所以第二个循环是为什么它崩溃的问题。
【问题讨论】:
-
我注意到
next->link上没有有效性检查。你可能会在那里得到一个空指针。 -
当您设置
current = temp;,然后设置delete temp;,那么current将指向一个不再存在的对象。稍后您访问current->link等,这肯定是导致崩溃的可能原因之一。 -
首先找出程序崩溃的确切行。
-
你删除了临时文件。只是评论它。有效吗?
-
改用
std::sort有什么问题?