Getting 'unhandled exception' error for no apparent reason

Hi everyone. Hope you're having a terrific Thursday evening. So anyway I've been working on my C++-language project as of late and having come quite far into fleshing out all the things I'd originally planned to include in my project, I have stumbled upon a nasty set of errors that despite my best efforts to resolve them continue to persist.

The error message that pops up every so often when I test things out for my project has this to say:
"Unhandled exception at 0x76f415de in thistimewereplayingforreal.exe: 0xC0000005: Access violation reading location 0xfdfe23b5."

Now, from what I understand, these type of errors are known to track the footprints of bad memory allocation, illegal access of memory nodes etc. What befuddles me to put it frankly is the fact that I can't seem to track down the root cause of the error in my program design.

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
Map defaultMap ( input );

		int a = 0;
		int b, c, d;
		d = 0;
		b = rand() % defaultMap.lengthOfAxis;
		c = rand() % defaultMap.lengthOfAxis;
		while ( a < input ) {

			if ( defaultMap.worldMap[c].identifier == 0 ) {
				defaultMap.worldMap[b][c].identifier = 1;
				a++;
				d = rand() % 4 + 1; // THIS LINE RIGHT HERE IS WHERE THE EXCEPTION OCCURS AT SOME POINT
					switch ( d ) {
					case 1:
						if ( ( b - 1 ) >= 0 )
							b--;
						break;
					case 2:
						if ( ( c - 1 ) >= 0 )
							c--;
						break;
					case 3:
						if ( ( b + 1 ) < defaultMap.lengthOfAxis )
							b++;
						break;
					case 4:
						if ( ( b - 1 ) < defaultMap.lengthOfAxis )
							b--;
						break;
					}
			}
			else {
				for ( int j = 0; j < defaultMap.lengthOfAxis; j++ ) {
					for ( int k = 0; k < defaultMap.lengthOfAxis; k++ ) {
						if ( defaultMap.worldMap[j][k].identifier == 0 ) {
							defaultMap.worldMap[j][k].identifier = 1;
							a++;
							j = k = defaultMap.lengthOfAxis;
							b = rand() % defaultMap.lengthOfAxis;
							c = rand() % defaultMap.lengthOfAxis;
						}
					}
				}
			}
		}
		// create the map, raise land and chuck in the seas 


My first thoughts were:"Perhaps the rand() procedure goes berserk and starts tampering with designated pieces of memory... though I have no clue why that would be true."
Do you guys have any ideas as to what I'm doing wrong here? Please don't hesitate to speak up.
Last edited on
What is lengthOfAxis? If that's a dimension of your array, if you are at that value you will be outside of the array.

[b]d
What the hell is this?

EDIT:
b is never initialized, that could very well be causing you issues also.
Last edited on
[b]d

This isn't proper syntax AFAIK. It seems strange that your compiler would not get angry.

I would tell you that the syntax for accessing array elements is like so:

array[index]

But neither 'b' or 'd' are arrays. What is that line in particular supposed to do?
Sorry about the confusion guys. I must have messed up whilst copying the code to the posting. There is no [b] segment in that line.

The correct spelling of that particular line is:
d = rand() % 4 + 1;

Also, as far as the variable named 'lengthOfAxis', it defines the size of my dynamically created array. I know I'll be outside of it in the event that the program attempts to read memory past the final item, that's why I have those cross-checks in place.
Last edited on
Then refer back to my first question. If lenfthOfAxis == the width or length of your array, you could very well run off the end. And be sure to initialize b before using it. Currently you use it to access your array before it is initialized.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Map {
public:
	Tile** worldMap;
	int lengthOfAxis;

	Map ( int sizeOfLand ) {
		lengthOfAxis = int ( sizeOfLand * 1.1 );
		worldMap = new Tile*[lengthOfAxis];
		for ( int j = 0; j < lengthOfAxis; j++ ) {
			worldMap[j] = new Tile[lengthOfAxis];
			for ( int k = 0; k < lengthOfAxis; k++ ) {
				worldMap[j][k].identifier = 0;
			}
		}
	}
};

Some more goodies up above. LengthOfAxis is very much initialized by the time it is run into in the succeeding lines.
Or are you saying I should throw in a 'b = 0;' statement somewhere in between?

EDIT: I must be as blind as a bat here. Could an uninitialized variable truly be causing me this much trouble?
Last edited on
I said b is not initialized.

And yea since worldMap is defined with size of lengthOfAxis, if your little random number gets placed at exactly that, you'll be off the array. You understand?

Edit, you got it. b could be anything if you don't initialize it. C++ isn't as nice to as other languages are.
Last edited on
Yeah I know what you're saying but the darn thing keeps telling me the exception has something to do with 'd', not 'b' or the 'c' integer for that matter. I'll give it a try though and start by initializing my variables this time around.

EDIT: Initializing them did not make the exception go away. What in Lord's name should I do?
Last edited on
You've ignored the main concern of mine each time.

Edit, nevermind d doesn't look to be accessing anything.
Last edited on
That's exactly right! All it does is let the rand() procedure seek out a number to go with.
I'm stumped. Have you tried stepping through and watching each of the values? The error message definitely is saying you're accessing memory you shouldn't be somewhere, which would be accessing an array out of bounds.
Last edited on
Oh yes I have and if that weren't bad enough, the exception doesn't throw on every occasion but only on a select few. I can't figure out why. Sometimes I'll run the code and the exception won't show up on my screen, others it will.
Then I'm almost certain it has to do with the variables that are assigned random values and access the array. Because even if it doesn't say so, those values can very well access out of bounds of your array. You need to adjust those values so they can't be assigned at size of the array. Do you understand?
Let's get a good look at the following line, shall we?
b = rand() % defaultMap.lengthOfAxis;

Suppose the axis length was assigned a value of 5... in that case, the above line would translate to this:
b = rand() % 5;

Which leaves it with no more than five possible outcomes for the resulting number... they are 0, 1, 2, 3 and 4 respectively. How can any of those gain access out of bounds when common sense dictates it would take a figure in the red or at least a 6 or 7 to do so? I just don't know what the heck the exception's for.

EDIT: "Unhandled exception at 0x76f415de in thistimewereplayingforreal.exe: 0x00000000: The operation completed successfully."
Well, this is confusing. So the operation did complete successfully, but wasn't able to warrant proceeding with execution of remaining code?
Last edited on
Topic archived. No new replies allowed.