Perché il “delete nodo;” Crash mia C ++ legato all'applicazione lista?
-
06-09-2019 - |
Domanda
Il metodo jDeleteAfter della mia classe lista collegata dovrebbe eliminare il nodo immediatamente dopo il nodo passato come argomento. Se si sta facendo, non lo so, ma è bruscamente chiudendo la mia applicazione console quando "delete TLP;" (Temp Lista Pointer) viene letto. Il mio istruttore, gli utenti di un forum di programmazione e devo ancora determinare la radice di questo problema.
Scritto in Dev-C ++ 4.9.9.2:
[source]
#include "JawaListT.h"
#include <cstdlib>
#include <iostream>
#include <new.h>
/*DEFAULT CONSTRUCTOR*/
JawaListT::JawaListT()
{
if((this->jHead = new (nothrow) JawaLinkT) && (this->jTail = new (nothrow) JawaLinkT))
{
this->jHead->jSetNext(this->jTail);
this->jTail->jSetNext(this->jTail);
}//end if allocated
}
/*INSERT NODE AFTER*/
void JawaListT::jInsertAfter(JawaLinkT* lp, int val)
{
if(lp != NULL && lp != this->jTail) //if passed not tail and not null
{
JawaLinkT* tlp; //new list node
if((tlp = new (nothrow) JawaLinkT) != NULL) //if dynamically allocated
{
tlp->jSetNext(lp->jGetNext()); //temp.next = passed.next
lp->jSetNext(tlp); //passed.next = temp
tlp->jSetValue(val); //temp.data = val
}//end if allocated
}//end if not tail
}
/*INSERT NODE BEFORE*/
void JawaListT::jInsertBefore(JawaLinkT* lp, int val)
{
if(lp != NULL && lp != this->jHead) //if passed not head and not null
{
JawaLinkT* tlp; //new list node
if((tlp = new (nothrow) JawaLinkT) != NULL) //if dynamically allocated
{
tlp->jSetNext(lp->jGetNext());
tlp->jSetValue(lp->jGetValue());
// *tlp = *lp; //copies passed node to temp node
lp->jSetNext(tlp); //passed.next = temp
lp->jSetValue(val); //passed.data = val
if(lp == this->jTail) //if passed is tail
{
this->jTail = tlp; //tail is temp
this->jTail->jSetNext(this->jTail); //tail.next = tail
}//end if lp
}//end if tlp
}//end if head
}
/*REMOVE NODE AFTER*/
void JawaListT::jDeleteAfter(JawaLinkT* lp)
{
if(lp != NULL && lp->jGetNext() != this->jTail) //if not tail and not null
{
JawaLinkT* tlp; //temp pointer to node
tlp = lp->jGetNext(); //temp = passed.next
lp->jSetNext(tlp->jGetNext()); //passed.next = temp.next
delete tlp; //delete to what temp points
}//end if next
/*code that did not work any better*/
// tlp->jSetNext((lp->jGetNext())->jGetNext());
// delete lp->jGetNext();
// lp->jSetNext(tlp);
/*Also tried declaring and/or deleting tlp outside of decision structure, and
jDeleteCurrent(tlp) since that function works properly.*/
}
/*REMOVE CURRENT NODE*/
void JawaListT::jDeleteCurrent(JawaLinkT* lp)
{
if(lp != NULL && lp != jHead && lp != jTail) //if not head or tail, not null
{
JawaLinkT* tlp; //temp pointer to node
tlp = lp->jGetNext(); //temp = passed.next
*lp = *tlp; //copy temp to passed
if(tlp == jTail) //if temp is tail
{
this->jSetTail(lp); //tail = passed
lp->jSetNext(lp); //passed.next = passed
delete tlp; //delete to what temp points
}//end if tail
}//end if not head
}
/*LINEAR SENTINEL SEARCH*/
JawaLinkT* JawaListT::jFindItemS(int item)
{
JawaLinkT* tlp; //temp pointer to node
this->jTail->jSetValue(item); //tail.data = item
for(tlp = jHead->jGetNext(); tlp->jGetValue() != item; tlp = tlp->jGetNext());
/*INIT: node after head, EXIT: data found, UPDATE: increment node*/
if(tlp == jTail) //if sentinel found
std::cout << item << " not in list" << std::endl;
return((tlp != this->jTail->jGetNext()) ? tlp : NULL);
/*If sentinel not found, return proper node, else return null*/
}
[/source]
Io uso la ricerca di classe sentinella per attraversare l'elenco e fornire il nodo corretto come argomento per jDeleteAfter.
Nessuna soluzione corretta
Altri suggerimenti
Un semplice suggerimento: rimuovere tutti i test per la mancata assegnazione - che non potrà mai accadere su una piattaforma Windows amd complicare il codice. E se accadono, non recuperare da loro, quindi i test sono doppiamente inutili.
Si scopre c'era un conflitto con la mia dichiarazione di eliminazione nella mia distruttore virtuale. Tutto ora funziona. Grazie per il look-over del mio codice.
Per quanto riguarda i nothrows - lo faccio in questo modo perché il nostro testo ha introdotto l'idea e non so come gestire ancora eccezioni. Grazie per il consiglio, però.
Alcuni consigli recensione codice:
JawaLinkT* tlp; //new list node
if((tlp = new (nothrow) JawaLinkT) != NULL)
è più leggibile come:
if(JawaLinkT* tlp = new (nothrow) JawaLinkT)
(anche, vedi il commento di Neil sopra perchè usando nothrow senza fare nulla)
Il codice è inoltre disseminato di perdite di memoria potenziale Casualmente:
if((this->jHead = new (nothrow) JawaLinkT) && (this->jTail = new (nothrow) JawaLinkT))
// What if first new succeeds and second fails?
Per quanto riguarda la domanda, questo è un sacco di codice per leggere senza nemmeno traccia dello stack per cercare solo un bug generica, ma penso jDeleteAfter può essere implementata in modo non corretto. Si consideri il caso in cui la funzione è passato il nodo prima della coda. Ti taglio fuori lì perché lo fa apparire come compiti a casa; ma se si sta ancora problemi, commentare e io chiarire.
EDIT: E mi sono reso conto che mi sbagliavo. Nevermind!