Results 1 to 4 of 4

Thread: Copying contents of a Type

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    73

    Copying contents of a Type

    What would be the most efficient way to copy the contents of a "Type" (a record structure).


    Apparently "RtlMoveMemory" does not work under all circumstances as in the following example:

    Code:
    Public Declare Sub CopyMemory Lib "kernel32.dll" _
                Alias "RtlMoveMemory" ( _
                Destination As Any, _
                Source As Any, _
                ByVal Length As Long)
        
    Public Type myRecord
        daKey As Byte
        daTime As Date
        daValue As Long
    End Type
    
    
    Private Sub copyRecords()
        Dim r1 As myRecord, r2 As myRecord
    
        With r2
            .daKey = 1
            .daTime = CDate("05:23:31")
            .daValue = 1723
        End With
        
        Call CopyMemory(r1, r2, Len(r1))
    End Sub
    I find that not every single record field is copied.

    (I guess the problem has to do with aligning data to DWORD boundaries?)


    So what would be the most efficient (and secure) way to copy the contents of a type?

    (Especially if there are many record fields it might be quite hard to copy every single record field with an assignment.)

  2. #2
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: Copying contents of a Type

    The contents of a variable of a given User Defined Type can be copied to another of the same Type with a simple assignment as in;

    r1 = r2

    CopyMemory is not required.

    Lset r1 = r2 may be slightly more efficient/ quicker.

  3. #3
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Copying contents of a Type

    In your code you need to use the LenB function instead of the Len function. LenB includes padding while Len doesn't. Remember that you can't CopyMemory UDTs that contain reference types (well, not so simply, at least).
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Copying contents of a Type

    I made some benchmarking on what Magic Ink said. I made a simple generic function that replicates the given data to all members of an UDT array. I got some surprising results.

    First, this one is clearly the fastest method to fill all the UDT items:
    Code:
    For I = 0 To Count - 1
        Records(I).daKey = daKey
        Records(I).daTime = daTime
        Records(I).daValue = daValue
    Next I
    While this is slower:
    Code:
        Records(0).daKey = daKey
        Records(0).daTime = daTime
        Records(0).daValue = daValue
        
        For I = 1 To Count - 1
            Records(I) = Records(0)
        Next I
    And it does not matter if you use LSet or not. I don't really know why this is slower, you would think this kind of assignment would be optimized by the compiler, but it isn't. I guess there is some level of "too intelligent" lookup done when copying a full UDT item to another. It doesn't even matter if you replace Records(0) with a local UDT variable.

    Out of interest I also made a "too advanced" function that replicates the first record to the whole array by using VB6's string replication routine. It was the fastest, but not by a great margin to be of any real value. Too long code for such a simple thing with no great gain.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width