Re: Question about 2D VB arrays




"John" <John@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:62313F40-A0FC-4E8A-A14D-2E925F38873A@xxxxxxxxxxxxxxxx
Dmitriy,

You state the following:

"So if you want to have the same memory layout in both languages, then
you
just
need to switch "meaning" of subscripts to the opposite - if you think
that
the first
dimension in C is a row, then you should think that the first subscript in
VB is a column."

When you stated this, it leads one to believe that one of the langues will
now be
more inefficient when accessing the array. If the array is stored in
contiguous
memory in column major order (i.e. VB) and accessed by C, the C code will
be much more inefficient, due to the fact that it expects it's data to be
in
row
major order. There is going to be a big hit due to memory access.

John


What do you mean? Do you mean that there is a difference in access time to
different elements of the same array? Shouldn't. But if you think so then
put VB code into "disadvantageous" position (from you point of view) - VB
code will not really suffer from such "treatment", especially comparing to
efficiency of C code.

Additionally, as it was mentioned here, you can use the same technique in VB
to avoid any confusion. This is direct (unlike your example) equivalent of
your C code in VB:

dim i&, j&, k&
dim larray(480& * 640 - 1) as long
for i=0 to 480 -1 ' loop for rows
for j=0 to 640 - 1' loop for columns
larray(i * 640 + j) = k' row * columns + column
next j
next i
erase larray

Unless I overlooked something, it will give you exactly the same memory
layout and the same access algorithm. To squeeze every CPU cycle I would
replace (640-1) with its result for the price of clarity (I am not sure that
VB compiler will optimize it).


And if we go back to 2D array in VB, you still, as I said, can achieve the
same result (same memory layout) if you change the meaning of dimensions. If
you thought that the first dimension is a row and the second is a column,
then just think opposite. For example in the following snippet I just
slightly changed your sample:

Dim i As Long, j As Long, k As Long
Dim larray(0 To 479, 0 To 639) As Long

k = 0
For i = 0 To 639 ' columns
For j = 0 To 479 ' rows
larray(j, i) = k
k = k + 1
Next j
Next i

It is a little bit late and there can be a lot of mess in my head, but it
seams to me that result will be the one that you want. Again, there should
be no any reputable documentation, which dictates you which dimension
represents rows and which one - columns - it's all about convention.

Dmitriy.


.



Relevant Pages

  • RE: Three Dimensional Array Question
    ... The same is true for dynamic allocation of memory. ... I haven't gotten an array to index from a string variable, ... "Tornados" wrote: ... > often and for each row i want the third dimension to act...so: ...
    (microsoft.public.excel.misc)
  • out of memory with reasonables sizes
    ... I`m dealing with 'out of memory' problems at the moment, ... The array is allocated and filled ... call an m-file I wrote, which updates the probabilty array, I get a ... dimension or else. ...
    (comp.soft-sys.matlab)
  • Size of Largest Array?
    ... Can anybody tell me the size of the largest array I can dimension? ... I get the idea that I can only use 2 gigs of memory. ...
    (comp.lang.fortran)
  • Re: Size of Largest Array?
    ... > Can anybody tell me the size of the largest array I can dimension? ... > I am using Lahey FORTRAN 90. ... > I get the idea that I can only use 2 gigs of memory. ...
    (comp.lang.fortran)
  • Re: equivalence with dummy arguments
    ... > What do you think that the main purpose of equivalence was? ... > using the same memory for different variables where practical. ... > with compile-time memory layout to save memory. ... What does the saving of memory have to do with *compile-time* ...
    (comp.lang.fortran)