Pointer array's addresses reset outside constructor

I'm having a problem with a 2d pointer array. Outside of the constructor, the addresses seem to get reset. I don't understand, exactly, what's happening. : (
What's got me so confused is that I've done similar things with pointers before. It's just all of a sudden I'm probably doing something really stupid.

This is how the class that uses the array is defined, followed by the two functions where the problem occurs:

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
63
64
65
class River
{
public:
	friend ostream& operator<<(ostream&, CROSSINGSYMBOL** crossing_table);
	friend ostream& operator<<(ostream& osObj, bool _river[]);
	
	River(void);
	void show(void);
	void cross(int);
	bool stop(void);

private:
	bool _river[HUSBAND1];
	void copy(bool[], bool[]);
	int state(bool[]);
	bool goodCrossing(bool load_river[], bool unload_river[]);
	CROSSINGSYMBOL** crossing_table;
	static int charToPossibility(char);
};
#endif

River::River(void)
{
	crossing_table = new CROSSINGSYMBOL*[SIZE];

	for(int row = 0;row <= SIZE - 1; row++){
		crossing_table[row] = new CROSSINGSYMBOL[SIZE];
	}

	ifstream input;
	input.open("transitiontable.txt");

	for (int loop1 = 0; loop1 <= SIZE -1; loop1++){
		string newString, tempString;
		newString = tempString = "";
		char tempChar = ' ';
		getline(input, newString);
		for(unsigned int loop3 = 0; loop3 <= newString.length() - 1; loop3 ++){
			tempChar = newString[loop3];
			if(tempChar != ' '){
				tempString = tempString + tempChar;
			}
		}
		newString = tempString;
		for(int loop2 = 0; loop2 <= SIZE -1; loop2++){
			crossing_table[loop2][loop1] = static_cast<CROSSINGSYMBOL>(charToPossibility(newString[loop2]));
		}
	}

	cout << crossing_table << endl;
	cout << &crossing_table[0][0] << endl;


	for(int pos = BOAT; pos <= HUSBAND1; pos++){
		_river[pos] = false;
	}

	input.close();
}

void River::show(void)
{
	cout << &crossing_table[6][6] << endl;
	cout << _river ;
}



1
2
3
for(int pos = BOAT; pos <= HUSBAND1; pos++){
   _river[pos] = false;
}


It looks like an array-out-of-bounds is happening here. The maximum index into the array _river should be HUSBAND1 - 1.
That part's fine. Husband1, etc, are part of an enum, so they're the actual numbers. ; (
You've declared _river like this:

bool _river[HUSBAND1];

This means that _river has size HUSBAND1 (I'm aware it's a constant, you just haven't shown its precise value).

This also means that the valid indeces when indexing _river are {0, 1, 2, 3, ..., HUSBAND1 - 1}. Your code indexes _river with the index HUSBAND1 (whatever that is), but that's the size of the array. Hence, it is an array-out-of-bounds which would invoke undefined behaviour in your program.

Change your loop to the following (notice the strictly less than '<' sign):

for(int pos = BOAT; pos < HUSBAND1; pos++)

and see if it fixes your issue.
Last edited on
Fascinating! You're totally right. The arrays come out right, but the last value in the enum is not set to false along within the loop, so I must change it to something like this:

{
cout << crossing_table << endl;

for(int pos = BOAT; pos < HUSBAND1; pos++){
_river[pos] = false;
}
_river[4] = false;

input.close();
}

That looks rather ugly to me, however. Is that okay, though?

I think I needed to create the array as HUSBAND1 + 1, since HUSBAND is part of an enum. When I cout << HUSBAND1, 4 is returned, as there are 5 objects in the enumeration, even though when I create arrays with ints as the index, that int would just be the size, while I'd index with "-1" in mind.

Does it make sense why I'm so confused?
I think I needed to create the array as HUSBAND1 + 1

Yes, it looks that way. A common trick with enums is to have the last value be the number of values, like so:

1
2
3
4
5
6
7
8
9
enum RiverObjects
{
    BOAT = 0,
    BATTLESHIP,
    CRUISER,
    SUBMARINE,
    HUSBAND1,
    NUM_RIVER_OBJECTS
};


So then you could declare your array like this:

bool _river[NUM_RIVER_OBJECTS];

so that index x into the array _river would match up with enum value x.

Does it make sense why I'm so confused?

Yes, we're taught since grade school to count from one then during our first programming class we're taught to count from zero.
Last edited on
Topic archived. No new replies allowed.