我在删除指针时遇到问题。我不认为我在做任何编译器非法或任何事情,但也许我是,所以如果有人能解释我的逻辑中的缺陷,我将不胜感激。我希望下面的函数应该足以提供帮助,因为整件事要转录很多,但是如果需要更多代码,请告诉我,我会添加它!
以下是从我创建的链接列表中删除储物柜的功能。我已尽力涵盖所有可能的情况。当我尝试实际释放要删除的储物柜的内存时,就会出现问题。我试图删除引用锁定的临时变量的行被注释掉了,因为包含它们的代码中断了。很明显,不过,没有它们,我不能像我想要的那样删除储物柜。
int SelfStorageList::removeLockersOverdue() {
int lockersDeleted = 0;
if (isEmpty()) {
return 0;
}
if (head->objLocker.isRentOverdue && head==tail) { //If that was the only locker, the tail needs to be updated to = head = 0
delete head;
head = tail = 0;
return ++lockersDeleted;
}
LockerNode *prev = head;
LockerNode *curr = head->next;
while (curr != 0) {
if((curr == tail) && curr->objLocker.isRentOverdue) { //If the current locker is tail and needs deleting
LockerNode *temp = curr;
curr = prev;
//delete temp;
lockersDeleted++;
}
if(prev->objLocker.isRentOverdue) { //General case: Previous locker needs deleting
LockerNode *temp = prev;
prev = prev->next;
curr = curr->next;
//delete temp;
lockersDeleted++;
}
else { //Update the pointers if not updated anywhere else
prev = prev->next;
curr = curr->next;
}
}
return lockersDeleted;
}
任何“指针”?(可怕的双关语。:p)