Zoltán Schmidt
Zoltán Schmidt

Reputation: 1345

Access violation reading while using object pointers

At the beginning, my program creates a structure and a class that contain a struct and two int variables.

#define fullwidth 200
#define fullheight 200

typedef struct tiles
{
    unsigned char red, green, blue;
    char* name;
}tiles;

class Units
{
public:

int X_Pos;
int Y_Pos;

tiles MapColour;

}

After that, in the main part, I create a 2-dimensional array to use tiles as RGB containers for display, and an array of object pointers to follow any changes in the declared objects.

int i, j;

tiles fieldd[fullwidth][fullheight];

Units* DetectorField[fullwidth][fullheight];

Units Objects[10];

After that (now in main()), I upload both of the arrays with valid values, avoiding issues about that.

for (j=0;j<fullheight;j++)
{
    for (i=0;i<fullwidth;i++)
    {
        fieldd[i][j] = BASE;
        DetectorField[i][j] = NULL;
    }
}

Same with the objects + adding object memory adress for pointers to be able to identify them through DetectorField:

for (i=0; i<9;i++)
{
Objects[i].X_Pos = i+2;    //just some values, not important yet
Objects[i].Y_Pos = 2*i+2;
DetectorField[Objects[i].X_Pos][Objects[i].Y_Pos] = &Objects[i];
}

Most certainly, this is okay yet. But the problem comes now! In the next piece of codes, I check every elements of DetectorField; if the chosen element isn't NULL yet (which obviously means that it can be only the memory adress of an object, since it couldn't get any other values - if I know well), then put the MapColour variable to the array of structures.

for (j=0;j<fullheight;j++)
{
    for (i=0;i<fullwidth;i++)
    {
        if(DetectorField[i][j] != NULL)
        {
            fieldd[i][j] = DetectorField[i][j]->MapColour;
        }
    }
}

At this point, MSVC gives this error message when I try to run it: Unhandled exception at 0x00411ed7 in Fallen Star.exe: 0xC0000005: Access violation reading location 0xccccccdc.

What did I do wrong? The operation inside the condition seems OK for me. Is the problem the way I use fieldd maybe?

Upvotes: 1

Views: 4139

Answers (2)

Juniar
Juniar

Reputation: 1399

You simply have access violation. You should debug your code, by Stepping Into and Stepping Over to find out where the access violation is coming from. The access violation says, you are trying to refer to NULL references. Make sure you allocate memory to your Objects before accessing them.

You have a for loop that assigns DetectorField[i][j] = NULL, meaning location [i][j] is a NULL reference. And then you have a reference DetectorField[Objects[i].X_Pos][Objects[i].Y_Pos]. You might have some access violation right there.

Upvotes: 2

llakais
llakais

Reputation: 1637

This loop:

for (i=0; i<9;i++)
{
    Objects[i].X_Pos = i+2;    //just some values, not important yet
    Objects[i].Y_Pos = 2*i+2;
    DetectorField[Objects[i].X_Pos][Objects[i].Y_Pos] = &Objects[i];
}

Does not seem to do a full initialization, because there are 10 elements in Objects and you only initialize 9 of them.

I think you want

for (i=0; i<10; i++)

Or even better

for (i=0; i<(sizeof(Objects)/sizeof(Objects[0])); i++)

Upvotes: 1

Related Questions