Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

for loops & 2d arrays

Guys,

is there anything stopping me from doing something like that described below

int size = 100;
BYTE *bits;
BYTE *map;
int x = 0;
int y = 0;
bits = new BYTE[ size ];
map = new BYTE [ 10, 10 ];

//Assume that I randomly fill the bits array with random numbers from 0 to
255 here

for (int i = 0; i < size; i++)
{
x = (i % size);
y = (i / size);

map[ x, y] = bits [i];

if ((map[ x,y ] < 10) && (map[( x-1),y] < 10))
{
//do something;
}
}


The problem I am having is whenever I test the value of map[(x-1),y) inside
the for loop, it always gives me the current value of map[x,y].

I have checked by setting breakpoints, writing the values to the debug
window etc and it confirms this. However, if I test any value of map[x,y]
outside the loop, I get the correct answer.

e.g suppose the iterating through the for loop, x = 5 and y = 2 and the
value at this point was 0 and the value at x = 4, y = 2 was 255,

I would get:

map[5,2] = 0 //which is correct
map[4,2] = 0 //it should be 255.

If I write out the values of map[] after the loop has finished, I get the
expected results.

This isn't a complete listing, I have done error checking etc to make sure
the coordinates aren't out of bounds etc.

If it makes any difference, this loop is in a WM_PAINT event, the program
compiles & runs under debug & release, I just don't get the expected
results, I'm using vc++ 2008 express.

I would appreciate any help.




0 Kudos
Message 1 of 2
(6,054 Views)

There's a few things going wrong here. First of all dynamically allocating 2D arrays is a bit of a hassle in standard C-style array notation. Here's a link to a forum that goes through this process.

http://ubuntuforums.org/archive/index.php/t-437275.html

 

Secondly, and where the problem really lies is that the notation for 2D arrays is this...

int array[x][y];

 

You are using array[x,y] and this is why you are getting unexpected results.

 

Regards,

 

Steven Zittrower

Applications Engineer

National Instruments

http://www.ni.com/support

0 Kudos
Message 2 of 2
(6,034 Views)