Linked list program crashes

Hey guys I'm following an example in Alex Allains jumping into c++ book and while the program does exactly what I want it to do in terms of printing out all the items x_cordinates for some reason it crashes! after it prints out the results any reason why you think it crashes here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  #include <iostream>

using namespace std;

struct SpaceShip{

      int x_cord;
      int y_cord;
      int weaponPower;
      SpaceShip* nextShip;

};

SpaceShip *head_enemies = NULL;

SpaceShip *getNewShip(){

     SpaceShip *ship = new SpaceShip;
     (*ship).x_cord = 0;
     ship->y_cord = 0;
     ship->weaponPower = 50;
     ship->nextShip = head_enemies;
     head_enemies = ship;
     return ship;

}

int main()
{

  SpaceShip *ship1 = getNewShip();
  SpaceShip *ship2 = getNewShip();
  ship1->x_cord = 10;
  ship2->x_cord = 20;

  SpaceShip *current = head_enemies;

  while(head_enemies != NULL){

         cout << current->x_cord << endl;
         current = current->nextShip;
  }

}
Last edited on
closed account (48bpfSEw)
No idea, why your code crashes.

Another point: if you allocate memory with "new" you should free them before you turn off the lights... ^^ ...


refactored:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>

using namespace std;

class SpaceShip {
public:
    int x_cord;
    int y_cord;
    int weaponPower;
    SpaceShip* nextShip;

    // Initialize data
    SpaceShip()
    {
        x_cord = 0;
        y_cord = 0;
        weaponPower = 0;
        nextShip = NULL;
    }
};

SpaceShip* getNewShip(SpaceShip* head_enemies)
{

    SpaceShip* ship = new SpaceShip;
    if (!ship)
        return NULL;

    (*ship).x_cord = 0;
    ship->y_cord = 0;
    ship->weaponPower = 50;
    ship->nextShip = head_enemies;
    return ship;
}

int main()
{
    SpaceShip* ship1 = getNewShip(NULL);
    SpaceShip* ship2 = getNewShip(ship1);

    try {
        if (!ship1)
            return -1;
        if (!ship2)
            return -1;

        ship1->x_cord = 10;
        ship2->x_cord = 20;

        for (SpaceShip* current = ship2;
             current != NULL;
             current = current->nextShip) {
            cout << current->x_cord << endl;
        }
    }
    __finally {
        delete ship1;
        delete ship2;
    }

    return 0;
}
for some reason it crashes!

Line 38 should be:
 
  while (current != NULL)

You don't change head_enemies in your loop (nor should you). It continues to point to the front of the list and therefore is true. You want to check current to determine if you've reached the end of the list.

@AbstractionAnon
Beat me to answering. Looking at the code I didn't catch the error, but upon debugging it and seeing what it pointed to I realized the error. Logged back on to see you already answered it. This code shows that you definitely have to be careful of infinite loops.

@adam2016
As AbstractionAnon pointed out your while loop was infinite (always true). So it incremented through the list and printed out your two ship's x coordinates, but then incremented to a spot that didn't exist. As soon as your code called cout << current->x_cord << endl; it crashed due to a memory leak because it was accessing something that simply wasn't there to access. Change your while statement to what AbstractionAnon said and it will fix the issue.
Last edited on
thanks guys and thanks for the explanation on why it crashed much appreciated
Topic archived. No new replies allowed.