Community discussion forum

CopyMemory and Arrays: Proper Use

This is a comment thread discussing CopyMemory and Arrays: Proper Use
  • 9 years ago

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

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 5 years ago

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

  • 5 years ago

    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.

  • 4 years ago

    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.

Post a reply

Enter your message below

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