Click to See Complete Forum and Search --> : initializing a array...


toobe
May 20th, 1999, 04:43 AM
i wanna just initialize a array of BOOL data as following,
BOOL myArray[12][11];



In the constructor i initialise the array like this:

CMyDoc::CMyDoc()
{
...........
for (int y; y<11;++y)
{
for(int x; x<12; ++x)
{
myArray[x][y]=FALSE;
}
}
....................
}



It seems to have no problem. But when i started debugging, i have found that myArray[][]
was comically allocated with such values like "-847851241" or "577". But some elements
in the array was correctly allocated with "0".

Why ???
help me.....Thanx...
ur toobe

:-)

Johan M
May 20th, 1999, 05:00 AM
I might have a clue.
Use the for {...} statements like this:

for (int y=0; y<11; ++y)
{
...



I believe that the 'x' and 'y' variables are unassigned and can be of any value.
It is sheer luck that some values in the array are '0'.

/Johan

Johan M
May 20th, 1999, 05:05 AM
Sorry...
I saw another mistake:
Do like this:

for (int y=0; y<12; y++)
{



Increase y afterwards. Otherwise, position [0] in the array won't be assigned.
Remember the array is indexed 0, 1, 2, ..., 11 if you initialize it with MyArray[12];

/Johan M

Dave Lorde
May 20th, 1999, 05:29 AM
> BOOL myArray[12][11];

This defines the array, reserving memory for it, but it doesn't initalise it. You have to initialise it yourself. The only time things get initialised to zero by default is when they are declared static.

With an plain 'C'-style array like this, you can either use for/next to loop through, initialising each element individually, or you can treat it like a block of memory and use memcpy or FillMemory to write the required values all at once (this last is only safe with plain 'C'-style arrays, *not* with array classes).

Dave

Doofus
May 20th, 1999, 07:38 AM
Try

CMyDoc::CMyDoc()
{
...........
for (int y=0; y<11; y++)
{
for(int x=0; x<12; x++)
{
myArray[x][y]=FALSE;
}
}
....................
}