1

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?

4
  • 4
    Save yourself some grief and replace "char * name" in tiles with std::string. Commented Jul 20, 2013 at 16:43
  • What are the values of i and j when the violation occurs? Commented Jul 20, 2013 at 16:47
  • Thanks for the tip; however, sadly if I use std::string, I get stack overflow. Anyway: even if I change fullheight and also fullwidth 1, it crashes. Commented Jul 20, 2013 at 17:04
  • Odd if it crashes with fullheight = fullwidth = 1, as the loops won't execute anything. Oh hang on - forget that! Commented Jul 20, 2013 at 18:03

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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++)

2 Comments

But arrays start counting at 0, isn't it? So in case of [10] at initialization, it counts from [0] to [9] - or not?
You run the loop while i is less than 9...you initialize Objects[0] through Objects[8]. You never initialize Objects[9]. If you used i < 10 or i <= 9 as the stop condition in the for loop, you would initialize Objects[9] as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.