Pointers to Class crashes, Bad Alloc

closed account (SwMDizwU)
I've been writing a crude Text Based adventure for practice, but I've run into this problem and I can't figure out how to fix the crashing. I'm afraid this problem is a bit above my skill level, so I'm going to try and get an answer here.

I'm going to try and only post the relevant things.
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
class Location{
public:
  Location *ConnectionNorth,
           *ConnectionSouth;

  void ConnectedNorth(Location room){ConnectionNorth = &room;}
  void ConnectedSouth(Location room){ConnectionSouth = &room;}

}

int main(){
  Location roomA;
  Location roomB;

  Location *currentRoom;

  roomA.ConnectedNorth(roomB);//room B is connected to A to the North
  roomB.ConnectedSouth(roomA);//room A is connected to B to the South

  currentRoom = &roomA;


while(playing){

 //get input

	if(input == wants to go north)
	  currentRoom = currentRoom->ConnectionNorth;
	  //tell user they're in room B
	if(input == wants to go south)
 	  currentRoom = currentRoom->ConnectionSouth;
	  //tell user they're in room A

}


The annoying part is my code works for room changes about 3 times before crashing, I assume it's because I'm doing something wrong with the pointers, but I lack the knowledge.

Any help would be greatly appreciated.
roomA.ConnectedNorth(roomB);
1
2
3
4
5
  void ConnectedNorth(Location room)
{
ConnectionNorth = &room;
}
 

currentRoom = &roomA;
1
2
3
4
while(playing){
	if(input == wants to go north)
	  currentRoom = currentRoom->ConnectionNorth;
}


The problem is in your while loop. With currentRoom pointing to roomA's ConnectionNorth variable, you are assigning roomB's address to the currentRoom pointer. If you select the "wants to go north option" again, you would be referencing roomB's ConnectionNorth variable which is not initialized.
Last edited on
closed account (D80DSL3A)
You have a problem with pointers having un assigned and/or invalid values.
You initialized just 1 of the 2 pointers for each roomA and roomB.The other should be assigned NULL (or 0 or nullptr), and then this value tested for before using the pointer.
To prevent the crashing:
// A constructor for Location class
Location(): ConnectionNorth(0), ConnectionSouth(0) {}

Then in main():
[code
if(input == wants to go north)
if( currentRoom ) currentRoom = currentRoom->ConnectionNorth;
//tell user they're in room B
if(input == wants to go south)
if( currentRoom ) currentRoom = currentRoom->ConnectionSouth;
//tell user they're in room A
][/code]
This will let you step off of the map and get stuck though. Can you think of a fix for that?

EDIT: Always correcting for keystrokes missed by my wireless keyboard.
Last edited on
closed account (SwMDizwU)
I was able to fix the crashing problem by sending in the Locations from main() as reference, but now I have another weird error

also my actual code is about 300 lines long, so I'm trying to keep the relevant things in.
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
class Location{
public:
  Location *ConnectionNorth,
               *ConnectionSouth,
               *ConnectionEast,
               *ConnectionWest;

  void ConnectedNorth(Location* room){ConnectionNorth = room;}
  void ConnectedSouth(Location* room){ConnectionSouth = room;}
  void ConnectedEast(Location* room){ConnectionSouth = room;}
  void ConnectedWest(Location* room){ConnectionSouth = room;}
}

int main(){
  Location roomA;
  Location roomB;
  Location roomC;

  Location *currentRoom;

  roomA.ConnectedNorth(&roomB);//room B is connected to A to the North
  roomB.ConnectedSouth(&roomA);//room A is connected to B to the South
  roomB.ConnectedEast(&roomC);
  roomC.ConnectedWest(&roomB);

  currentRoom = &roomA;

/*    B -- C
      |
      A           */
while(playing){

 //get input
   	if(input == wants to go north)
           if(can go north)
	      currentRoom = currentRoom->ConnectionNorth;
           else //say cant go
        if(input == wants to go south)
 	   if(can go south)
              currentRoom = currentRoom->ConnectionSouth;
           else //say cant go
        if(input == wants to go east)
           if(can go east)
              currentRoom = currentRoom->ConnectionEast;
           else //say cant go
        if(input == wants to go west)
           if(can go west)
              currentRoom = currentRoom->ConnectionWest;
           else //say cant go
}

I also added the null values into my constructor (I didn't show it, I didn't think it was nessecary)
and have values to check if there is an available room

if I start from A I can go to B and back to A as much as I want,
then if I can go from B to C, but If I try to go back to A after being in C
when I go south it puts me in a strange Location that I didn't create and all available locations crash it.

so, A->B->A->B->C->B->(Strange area that isn't A)->Crash

any insight?
Last edited on
Topic archived. No new replies allowed.