Fast array copy in VB6 - tip

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Here's a quick tip for copying one array to another in Visual Basic 6.

Here's the technique: What you want to do is call the Win32 CopyMemory API.

For the first and second parameters, specify destination and source. You'll want to specify the lower most bound of the array in each of its dimensions. Thus the LBounds. That's where the actual array variable itself starts in memory. In other words, that's the array's starting address. You'll specify this for both of the parameters, first being source array and second being destionation array.

The third parameter in CopyMemory is more complicated. This specifies how much memory to copy. The formula is: Length = (Size of Variable) * (Total Number Of Items In All Dimensions) Length is specified in bytes for the CopyMemory function.

So for a one dimension array you'd just have the size of the variable multiplied by the number of entries in the array.

For a two dimensional like the example, you have to multiply the bounds of the dimensions by each other. Same thing for three or more dimensional. Each dimension of the array adds a lot of size. The example demonstrates copying a 2 dimensional array.

Anyhow, to get the size of the variable, use the LenB function. This measures how big each Integer variable is and returns it in a unit of bytes. For VB, that's 2 bytes, or 16 bits. It's generally 4 bytes, 32 bits in C. You should still use LenB incase a future VB6 runtime changes the size of the Integer. It's a fool-proof method. Also you don't have to use an Integer. Use a String, Long, whatever you want. LenB will always measure the size of it correctly.

LenB(OutRamp(LBound(OutRamp, 1), LBound(OutRamp, 2)))

Here's what happening: I'm taking the first value (LBound) of each dimension in the array. (I could take the size of the second item too and it wouldn't matter if the array was that big.) This gives me the size of Integer, what the whole array is declared as.

Then, get the number of items possible in dimension 1 of the array.

((UBound(OutRamp, 1) - LBound(OutRamp, 1)) + 1)

What I'm doing here is counting the number of items in the array. The reason I can't just use the returned upper bound is because the lower bound doesn't have to be 0 in VB. This will measure the number of items in that dimension (1) correctly each time.

The next section of code is the same thing except for the next dimension, 2, in a 2D array.

((UBound(OutRamp, 2) - LBound(OutRamp, 2)) + 1)

All these are being multiplied by each other. VariableSize * NumItemsInDimension1 * NumItemsInDimension2. If you have a 3D array, you'll get the number of items in the 3rd dimension and multiply it on top of the first two dimensions and variable size. Likewise for a 4D array you'll need to get the 1st,2nd,3rd,and 4th dimensions and multiply them all together with variable size. For just a regular old 1D array, it's just VariableSize * NumItemsInDimension1.

In the end, you finally have the length of the data you want to copy. The size of the variable multiplied by every single possible item address in the array.

The attached example gets the gamma for your display, puts it in an array, and then copies it to a second array (potentially for gamma adjustment). It doesn't change the gamma on your display or anything, it just gets the data and puts it in one array to copy to the other.

For the example, the end resulting called function is just this:

CopyMemory OutRamp2(0,0), OutRamp(0,0), 2 * 256 * 3
(0,0) being starting address. 2 being variable size of Integer. 256 being number of items in the first dimension (0 through 255). 3 being number of items in the second dimension (0 through 2). It's easier than it looks.

That's all folks.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
In C there has always been memcpy(source,dest);
In C++ there are standard functions built into the Vector for copy one to another.


Why is Basic so far behind the times?
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Just make sure it's an array of scalar values. I think in practice it is a bad idea to bulk copy containers in a managed environment like VB.