Re: Question about 2D VB arrays
- From: John <John@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 6 Nov 2006 09:12:02 -0800
Michael,
You state that the following is a 1D array and not a 2D array, this all lies
in your
perspective
LONG i, j, k = 0;
LONG* larray = new LONG[480* 640];
for (i=0; i<480; i++) { // loop for rows
for (j=0; j<640; j++) { // loop for columns
larray[i * 640 + j] = k; // row * columns + column
}
}
delete [] larray;
There is no difference from the code above and the code below, except the
code above is far more efficient. In the first example the array is
allocated in
contiguous memory space where the following code may or may not:
LONG i, j, k = 0;
LONG** larray = new LONG*[480];
for ( i = 0; i < 480; i++ )
l2d[i] = new LONG[640];
for ( i = 0; i < 480; i++)
for ( j = 0; j < 640; j++)
l2d[i][j] = k++;
for ( i = 0; i < row; i++ )
delete [] l2d[i];
delete [] l2d;
A lot of C/C++ programmers use the second method for ease-of-use. But
allocation in this manner requires a lot more overhead. Take for instance if
you want to get the sum of all elements of the array, what is more efficient
this?
double dsum = 0;
for (i=0; i<480*640; i++) // loop for total elements
dsum += *(larray++); // take sum
larray -= 480*640; // rewind pointer
or this?
for (i=0; i<480; i++)
for (j=0; j<640; j++)
dsum += l2d[i][j];
And on a side note, the reason I am using a 480 row 640 column array has
nothing
to do with the screen. I use these dimensions with respect to a CCD detector
that has 640 columns x 480 rows with each element being a pixel.
John
"Michael C" wrote:
"John" <John@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message.
news:9B1BA774-2D4E-4C7D-8026-B4F909F2115A@xxxxxxxxxxxxxxxx
This following question has started several "heated" discussions within my
company between the C/C++ and VB programmers and it deals with 2d visual
basic arrays.
Say for instance that I want to define an array that consists of 480 rows
by
640 columns. In C++ you would allocate and populate the array in the using
the
following syntax:
LONG i, j, k = 0;
LONG* larray = new LONG[480* 640];
for (i=0; i<480; i++) { // loop for rows
for (j=0; j<640; j++) { // loop for columns
larray[i * 640 + j] = k; // row * columns + column
}
}
delete [] larray;
In memory the array is stored in the following manner
The array is stored in any way you define it in that case. That is a 1D
array, not a 2D array. It also seems to be back to front to me. A screen is
640 x 480 not 480 x 640. I thought everything went x THEN y.
Hex
0x0000 0, 1, 2, 3, 4, 5......639 (Row 1)
0x0A00 641, 642........... (Row 2)
Now when I look at how these arrays are allocated and populated within our
company in VB it's done in the following manner
Dim i as long, j as long, k as long
Dim larray (0 to 639, 0 to 479) as long
k = 0
For i = 0 To 639 // columns
For j = 0 to 479 // rows
larray(i, j) = k
k = k + 1
Next J
Next I
The VB code above seems wrong, allocated wrong and populated wrong! They
allocate the array by columns then rows. All the documentation I have
found
Your for loop is back to front but that has nothing to do with VB. The rows
should be on the outside. Underneath the allocation should be the same,
shouldn't it?
Michael
- Follow-Ups:
- Re: Question about 2D VB arrays
- From: Michael C
- Re: Question about 2D VB arrays
- Prev by Date: Re: Printing Barcode (Code 128)
- Next by Date: Re: Question about 2D VB arrays
- Previous by thread: Printing Barcode (Code 128)
- Next by thread: Re: Question about 2D VB arrays
- Index(es):
Relevant Pages
|
Loading