Library tutorials & articles
CopyMemory and Arrays: Proper Use
Introduction
It is no secret VB is more often than not, a language that produces relatively slow machine code. This is the price we, VB programmers, pay for its extremely ease of use. But there have always been people trying to improve this slowness intrinsic in all VB programs. An example of this is the use of Windows API functions. They provide extremely fast access and processing of data. Of course, once you start using that, you stop getting a few benefits from the Visual Basic libraries, such as automatic reallocation of buffers and such. But hey, I think it is an OK price for what we get. I completely support the use of APIs and tricks in VB. They are good improvements to VB, and they are fun (most of the time).
In this small article, I will be showing how to use the CopyMemory API sub (known as rtlMoveMemory) to keep an array sorted all the time without much hassle, and by hassle I mean a For..Next loop. Such a loop is normally fast, but when you are handling lots of data, and you continously insert and remove items, this loop becomes a bottleneck for the program flow.
Related articles
Related discussion
-
Visual Studio: MDI Parent Form designer
by cutiegigshorty (0 replies)
-
Working on Who Wants To Be A Millionaire Project in VB6 [NEED HELP]
by candienne092 (2 replies)
-
Needs help with MSFlexGrid and MSAccess in VB6
by ClarkKentNeedsHelp (0 replies)
-
Make me or point me to an installer with comctl32 vb6
by tgkprog2 (0 replies)
-
How to use VB6 to make cumulative frequency polygons
by heibaidoufu (0 replies)
Thanks. Definately some good information, however...
It is not very practical to use the ReDim statement each iteration of a loop since it copies the entire array to a different memory location in order to change the dimension. In fact, using the CopyMemory function for this purpose is useful as well. A good compromise would be to redimension the array every 20th iteration and then check for empty array values, such as:
If SubDirCount = UBound(DirList) Then ReDim Preserve DirList(UBound(DirList) + 20)
an even better solution is to use an array object.
I just tested and yes, you are correct.
Private Type MyType
var1 As Integer
var2 As Byte
var3 As Boolean
End Type
The above type will get padded to a WORD, not a DWORD. However, starting with a WORD is not enough. The following will get DWORD-aligned, meaning you must not have DWORDS or QWORDS.
Private Type MyType
var1 As Integer
var2 As Byte
var3 As Long
End Type
Private Type MyType
var1 As Integer
var2 As Byte
var3 As Double
End Type
Private Type MyType
var1 As Integer
var3 As Double
End Type
Thanks for the observation. I will update the article to add the finding.
I'm willing to bet that if he first variable in the UDT had been an integer, the following Bytes would have been padded to WORD size and no more.
This being the start of the week-end, I'll play with this later...
This thread is for discussions of CopyMemory and Arrays: Proper Use.