Variable not found?

In my class, I have multiple variables that are defined in a header, and are used elsewhere in the functions that use them. However, whenever I call a function or variable from within the function definition, I'm getting errors saying that it can't find the variable, even though there aren't any errors coming up at compile-time.

I've added a cout in the constructor for the class, which shows the address for all of these variables. Here is the error that is being shown:

Unhandled exception at 0x002442b6 in Fireworks.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.

Here are the variables and their locations:

X: 001B4F10
Y: 001B4F12
TARGET_X: 001B4F16
TARGET_Y: 001B4F18

And none of these memory locations match up to any of the locations pointed to by the compiler.

My class, the constructor, and the variables:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class PARTSYS{
	public:
		PARTSYS( short sx, short sy, short targetx, short targety ){
			x				=	sx;
			y				=	sy;
			target_x		=	targetx;
			target_y		=	targety;
			std::cout
				<< "X: " << x
				<< "\nAt address: " << &x
				<< "\nY: " << y
				<< "\nAt address: " << &y
				<< "\nTARGET_X: " << target_x
				<< "\nAt address" << &target_x
				<< "\nTARGET_Y: " << target_y
				<< "\nAt address " << &target_y
				<< std::endl;
		}

		void process( void );

		short int x, y, target_x, target_y;

};


The process( ) declaration was in there too, since that's the function that the error is being called from. This function is being called by a static function of a different class, which uses a pointer returned by another static function of that class to point to an object of this class (PARTSYS):

1
2
3
4
5
6
7
8
9
10
class MANAGER{
	public:

		static PARTSYS* get_slot( int index );
		static void process( );

		static PARTSYS *first_slot;
		static int size;

};


And their definitions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
PARTSYS* MANAGER::get_slot( int index ){
	
	if( index < 1 || index > size )
		return 0;

	PARTSYS* ret = first_slot;

	for( int i=1; i<=index; i++ ){
		ret = ret->child;
	}

	return ret;
}

void SYS::MANAGER::process( ){
	for( int i=1; i<=size; i++ ){
		get_slot( i )->process( );
	}
}


And then the problem function is defined using:

1
2
3
void PARTSYS::process( ){
	...
}


All variables and functions that belong to PARTSYS are being errored, so I don't really have to show what's in that function.

What am I doing wrong here?



P.S.- As a side note, I've also noticed that even though y is defined as type short int, it's taking up 4 bytes. Any reason as to why this might be?
Last edited on
The error message says that you're trying to dereference an uninitialized pointer. Post the contents of get_slot().

Lines 6 and 7 in your first snippet do nothing. You're not assigning to the member, you're assigning the parameter to itself.

even though y is defined as type short int, it's taking up 4 bytes
The sizes of types is not guaranteed to be anything. A short could be an 8-bit integer in one implementation and a 64-bit integer in another.
Oh, and no, it's taking 2 bytes.
get_slot has been posted.

1
2
3
4
5
6
7
8
9
10
11
12
13
PARTSYS* MANAGER::get_slot( int index ){
	
	if( index < 1 || index > size )
		return 0;

	PARTSYS* ret = first_slot;

	for( int i=1; i<=index; i++ ){
		ret = ret->child;
	}

	return ret;
}


I'm pretty sure that the names targetx and target_x are different enough to be seen as different variables by the compiler.

And thanks for answering that little snippet at the end. Is it safe to assume that the MINIMUM size of a short would be 2 bytes?
I'm pretty sure that the names targetx and target_x are different enough to be seen as different variables by the compiler.
You're right. I didn't notice the underscore.

Do this: Run the program again and post the function that's closest to the top of the stack (it's possible the one at the very top is not yours). That may give some clue as to what is being dereferenced.
The.. stack? I don't know what you mean. I see a Stack Frame menu at the top, you mean that? Or do you mean the call stack

I'm using Visual C++ 2008, by the way. Just in case that helps in any way.
Last edited on
I'm a bit suspicious of the static PARTSYS *first_slot; pointer
I'm initializing it as 0 in my main function... although that might be the problem. I would think that it would stop me at MANAGER::process( ). Well, it did, but after I put the namespace name in front of it it worked fine. I HAD used 'using namespace SYS;' though... I'll check it out.

EDIT: Alright, I tested it, and it turns out that the get_slot function wasn't returning a valid address, and was returning cdcdcdcd, which I suppose is why that part of the memory is turning up. I'll look into the functions for adding slots, and I'll post if I get it.

EDIT: It looks like the fault of the get_slot function. It continually goes to the child of the last slot, and the default value for the pointer to the child is never initialized, and so it returns an invalid pointer. Thanks for the help!
Last edited on
Topic archived. No new replies allowed.