Checking array of object for "magic number"?

I have a class Figure and from that class I have derived basic figures in chess (pawn, king, etc). I need to simulate a chess board so I created an array as:

Figure* chess[8][8];

I did that so that the chess board will be at first uninitialized (it points to address 0xccccccc). In my program I initialize some spaces with pawns etc. But if i try to writte a loop to print out all the positions of pieces it reports an error because it tries to read from invalid location (0xcccc). I tried with if statement:

if(chess[i][j]!=NULL) { print();}; //exemple function

so that if it comes to unitialized space to ignore it but it doesn't work.

My guess is that i didn't check the adress of chess pointer correctly, but I don't know any other way.

Thank you for your time :)
if invalid pointer is equal to 0xcccccccc, then to check if a pointer is invalid, compare it to 0xcccccccc, not 0.
Though you should use 0 for invalid pointers. That's standard. Also, 0xcccccccc may actually point to something you need.
0xCCCCCCCC is Microsoft compiler code for unitialised pointer (you may also see some variation of this 0xCDCDCDCD and so forth).

I can't see why he cannot set the initial pointer array to NULL :

Figure* chess[8][8] = {0}; //initialise all the pointers to 0

Thank you both. It look's much safer if I just initialize all with 0, than to check all for 0xcccccc. I always complicate things :\

EDIT: I can't initialize chess in the class (it's a member of class Board). How can I initialize it in the constructor of the class Board?

1
2
3
4
class Board {
Figure* chess[8][8]  //doesn't alow initalization
public:
Board () { chess={0}; }; //error 


Is there a faster way than writting two loops to initalize all to 0?
Last edited on
Oops - Didn't realize the array was a member of a class.

In the constructor just initialise the array the long way around - using a nested loop - For example;
1
2
3
4
5
6
7
Board () 
{ 
    for (int row =  0; row < 8;  ++row)
     for (int col =0; col < 8; ++col)
        chess[row][col] = 0;

}
there is memset
Topic archived. No new replies allowed.