c++ - Random Integers Appear When Deleting Element in Linked List -


when attempt delete element linked list, of elements end somehow obtaining random values in place of originals. problem?

void removespaceshippower(spaceship *x, int y) {     spaceship *current = x;     spaceship *todelete = null;     spaceship *next = null;     while(current != null)     {         if(current->nextship->power == y)         {             next = current->nextship->nextship;             current->nextship = next;             todelete = current->nextship;             free(todelete);             return;         }         current = current->nextship;     } } 

main:

int main() {     spaceship *freelist = new spaceship;     freelist->power = 50;     freelist->health = 100;     spaceship *freelist2 = new spaceship;     freelist2->power = 25;     freelist2->health = 100;     spaceship *freelist3 = new spaceship;     freelist3->power = 75;     freelist3->health = 100;     spaceship *freelist4 = new spaceship;     freelist4->power = 100;     freelist4->health = 100;     freelist->nextship = freelist2;     freelist2->nextship = freelist3;     freelist3->nextship = freelist4;     freelist4->nextship = null;     printspaceship(freelist);     cout << getlowestpower(freelist) << endl;     removespaceshippower(freelist, 75);     printspaceship(freelist); } 

output:

spaceship #1 power: 50 health: 100 spaceship #2 power: 25 health: 100 spaceship #3 power: 75 health: 100 spaceship #4 power: 100 health: 100 25 spaceship #1 power: 50 health: 100 spaceship #2 power: 25 health: 100 spaceship #3 power: 9310088 health: 9306304 

i tweaked code generate this:

spaceship* removespaceshippower(spaceship *x, int y) {     spaceship *current = x;     spaceship *todelete = null;     spaceship *next = null;     // check if head of list deleted     if (current->power == y) {        next = current->nextship;        free(current);         return next;     }       while(current->nextship != null)     {         if(current->nextship->power == y)         {             todelete = current->nextship;           // remember address later             next = current->nextship->nextship;             current->nextship = next;               // splice out node deleted             free(todelete);                         // delete spliced node             return;         }         current = current->nextship;     }      return x; } 

here wrong original logic:

next = current->nextship->nextship; // next 2 steps ahead of current current->nextship = next;           // current->nextship points 2 steps ahead todelete = current->nextship;       // deleting wrong node! free(todelete); 

in graphical terms, assume had following linked list:

1 -> 2 -> 3 -> 4 -> 5 

if trying delete 3, original code have ended this:

1 -> 2 -> 3 -> null     -> 5 (dangling) 

the reason seeing random integers accessing memory had been freed. have had segmentation fault instead.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -