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.

Comments

  1. 26 Jun 2004 at 07:49

    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.

  2. 01 Mar 2003 at 13:25

    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.

  3. 01 Mar 2003 at 11:03

    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...

  4. 01 Jan 1999 at 00:00

    This thread is for discussions of CopyMemory and Arrays: Proper Use.

Leave a comment

Sign in or Join us (it's free).

Jose Pablo Ramirez Vargas

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!