Inter-class pointer issues

Started by
2 comments, last by ToohrVyk 19 years, 1 month ago
So I have a base class w/ a struct in it, I inherit it off to 2 more classes, which in turn create pointer objects each. One I create a whole new arrays of information, the other I assign it the memory reference of the other in the other class w/ the data. So when I go to assign an integer variable the value of it, it comes up with a run-time error during execution: System.NullReferenceException. It compiles just fine, using debug mode I can see the values are there. How come when it hits the part to read the int value from the pointed to int array it crashes?

int temp = Map[y].tile_ID[x]; //<---pointer error crap going on
I can see in debug mode that the values of tile_ID[x] is there, but it still crashes.

struct sMap{ int *tile_ID; bool *clip; };
Map is a struct array and tile_ID is a int array. Help please! =(
-Conrad
Advertisement
Obvious question I admit: are you sure that tile_ID points to a correctly allocated array of integers? int * tile_ID will not allocate any memory by itself, you have to create your own array in memory and have tile_ID point to it. (Same question for Map).
Yes, as I said, I can see the values properly in RAM during a debug sessession. Wouldn't you think I'd get a compiler error if I didn't? Converterting a int* to int doesn't go smoothly usually.
-Conrad
This kind of call crashes if you read memory that doesn't belong to you. This can happen if one of your pointers points to memory that hasn't been allocated, or if one of your indices is out of bounds.

Also, a compiler cannot check if you always initialize your values correctly before using them. This is the job of a static semantical analysis program, which costs a lot and takes hours to complete.

This topic is closed to new replies.

Advertisement