Duplicate array using copymemory-VBForums
Results 1 to 10 of 10

Thread: Duplicate array using copymemory

  1. #1

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    673

    Duplicate array using copymemory

    Hi

    Is there some way to duplicate informations of the array , I tried to use a example found using copy memory, but in example only copy data array to other. I would like some as:

    Myarray (0) = "A00"
    Myarray (1) = "A00"
    Myarray (2) = "B00"
    Myarray (4) = "AB0"

    When I call the function to duplicate array , I would like return me a array as:
    Myarray (0) = "A00"
    Myarray (1) = "A00"
    Myarray (2) = "B00"
    Myarray (4) = "AB0"
    Myarray (5) = "A00"
    Myarray (6) = "A00"
    Myarray (7) = "B00"
    Myarray (8) = "AB0"


    See the code below , I tried, but not found a way

    Code:
    Option Explicit
    Private Declare Sub CopyMemory Lib "kernel32" _
              Alias "RtlMoveMemory" ( _
              dest As Any _
            , source As Any _
            , ByVal bytes As Long)
    Dim MyArr() As Long
    Private Sub redimFast( _
              ByRef theArray() As Long _
            , ByVal LowBound As Long _
            , ByVal UpperBound As Long)
    ' copy A() into B()
    Dim tmpArr() As Long
    ReDim tmpArr(UpperBound - LowBound)
    CopyMemory tmpArr(0), theArray(LowBound), ((UpperBound - LowBound + 1) * Len(theArray(0)))
    
    Erase theArray
    theArray = tmpArr
    End Sub
    How to do it when my type as string

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,138

    Re: Duplicate array using copymemory

    Well, you have to change the sub header portion ByRef theArray() As Long to String instead of Long
    same with tmparray you can't put strings into an array of longs

  3. #3

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    673

    Re: Duplicate array using copymemory

    Quote Originally Posted by DataMiser View Post
    Well, you have to change the sub header portion ByRef theArray() As Long to String instead of Long
    same with tmparray you can't put strings into an array of longs
    Thank

    I tried as below, but I do know How can duplicate
    Code:
    Private Type mytype
        f01 As String * 3
        s01 As String * 1
        f02 As String * 3
        s02 As String * 1
        f03 As String * 3
        s03 As String * 1
        f04 As String * 3
        s04 As String * 1
        f05 As String * 3
        s05 As String * 1
        f06 As String * 3
        s06 As String * 1
        f07 As String * 3
        s07 As String * 1
        f08 As String * 3
        s08 As String * 1
        f09 As String * 3
        s09 As String * 1
        f10 As String * 3
        s10 As String * 1
        f11 As String * 3
        s11 As String * 1
        f12 As String * 3
        s12 As String * 1
        f13 As String * 3
        s13 As String * 1
        f14 As String * 3
    End Type
    Type mytype_full
       t_reg As String * 55
    End Type
    Dim MyArr() As Long
     Dim Arr1(0 To 1000) As mytype_full
     Dim Arr2(0 To 1000) As mytype
    ARR2(0)="AAB AAB AAC AAD AAB AAC CCC AAB AAB AAC AAD AAB AAC CCC"

    How can I to duplicate ?

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    16,500

    Re: Duplicate array using copymemory

    Problem you must understand with strings is that you are not just copying byte data, VB string arrays are arrays of BSTRs. Each string must be allocated. But all is naught since you appear to not be copying consecutive ranges of array items, but skipping some, i.e., (0),(1),(2),(4)

    If you are copying consecutive array elements, then this can be done, but honestly shouldn't be too much faster than (if at all, depending on size of array) than the following without using copymemory:
    Code:
    Dim lCount As Long, UB as Long, LB as Long
    
    LB = LBound(StringArray)
    UB = UBound(StringArray)
    lCount = Abs(UB - LB) + 1
    
    ReDim Preserve StringArray(LB To LB + lCount * 2 - 1)
    For UB = UB + 1 To UB + lCount
        StringArray(UB) = StringArray(LB)
        LB = LB + 1
    Next
    And with CopyMemory...
    Code:
    Private Declare Sub FillMemory Lib "kernel32" _
              Alias "RtlFillMemory" ( _
              dest As Any _
            , ByVal Length As Long _
            , ByVal Fill As Byte)
    
    Dim lCount As Long, UB as Long, LB as Long
    Dim tmpS() As String
    
    LB = LBound(StringArray)
    UB = UBound(StringArray)
    lCount = Abs(UB - LB) + 1
    
    tmpS = StringArray
    ReDim Preserve StringArray(LB To LB + lCount * 2 - 1)
    
    CopyMemory ByVal VarPtr(StringArray(LB + lCount)), ByVal VarPtr(tmpS(LB)), lCount * 4
    FillMemory  ByVal VarPtr(tmpS(LB)), lCount * 2, 0
    Note: The LB & UB stuff in sample code is simply to handle any LBound vs. hardcoding for zero
    The above is air-code, so apologize in advance if any typos

    Edited: You added another post, different data. The above is provided for your original post #1.
    Last edited by LaVolpe; Mar 16th, 2015 at 02:13 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VB and DPI Tutorial} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    673

    Re: Duplicate array using copymemory

    Quote Originally Posted by LaVolpe View Post
    Problem you must understand with strings is that you are not just copying byte data, VB string arrays are arrays of BSTRs. Each string must be allocated. But all is naught since you appear to not be copying consecutive ranges of array items, but skipping some, i.e., (0),(1),(2),(4)

    If you are copying consecutive array elements, then this can be done, but honestly shouldn't be too much faster than (if at all, depending on size of array) than the following without using copymemory:
    Code:
    Dim lCount As Long, UB as Long, LB as Long
    
    LB = LBound(StringArray)
    UB = UBound(StringArray)
    lCount = Abs(UB - LB) + 1
    
    ReDim Preserve StringArray(LB To LB + lCount * 2 - 1)
    For UB = UB + 1 To UB + lCount
        StringArray(UB) = StringArray(LB)
        LB = LB + 1
    Next
    And with CopyMemory...
    Code:
    Private Declare Sub FillMemory Lib "kernel32" _
              Alias "RtlFillMemory" ( _
              dest As Any _
            , ByVal Length As Long _
            , ByVal Fill As Byte)
    
    Dim lCount As Long, UB as Long, LB as Long
    Dim tmpS() As String
    
    LB = LBound(StringArray)
    UB = UBound(StringArray)
    lCount = Abs(UB - LB) + 1
    
    tmpS = StringArray
    ReDim Preserve StringArray(LB To LB + lCount * 2 - 1)
    
    CopyMemory ByVal VarPtr(StringArray(LB + lCount)), ByVal VarPtr(tmpS(LB)), lCount * 4
    FillMemory  ByVal VarPtr(tmpS(LB)), lCount * 2, 0
    Note: The LB & UB stuff in sample code is simply to handle any LBound vs. hardcoding for zero
    The above is air-code, so apologize in advance if any typos

    Edited: You added another post, different data. The above is provided for your original post #1.
    Thank you very much

    How can I to to duplicate and to triple...etc

    I tried

    Code:
    Dim lCount As Long, UB as Long, LB as Long
    
    LB = LBound(StringArray)
    UB = UBound(StringArray)
    lCount = Abs(UB - LB) + 1
    
    ReDim Preserve StringArray(LB To LB + lCount * 3 - 1)
    For UB = UB + 1 To UB + lCount 
        StringArray(UB) = StringArray(LB)
        LB = LB + 1
    Next
    But no worked :-(

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    16,500

    Re: Duplicate array using copymemory

    Code:
    Dim lCount As Long, UB as Long, LB as Long
    Dim nrCopies As Long
    
    LB = LBound(StringArray)
    UB = UBound(StringArray)
    lCount = Abs(UB - LB) + 1
    
    nrCopies = 2
    
    ReDim Preserve StringArray(LB To LB + lCount * (nrCopies+1) - 1)
    For nrCopies = 0 To nrCopies - 1
        For UB = UB + 1 To UB + lCount 
            StringArray(UB) = StringArray(LB)
            LB = LB + 1
        Next
        LB = LB - lCount
    Next
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VB and DPI Tutorial} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    673

    Re: Duplicate array using copymemory

    Quote Originally Posted by LaVolpe View Post
    Code:
    Dim lCount As Long, UB as Long, LB as Long
    Dim nrCopies As Long
    
    LB = LBound(StringArray)
    UB = UBound(StringArray)
    lCount = Abs(UB - LB) + 1
    
    nrCopies = 2
    
    ReDim Preserve StringArray(LB To LB + lCount * (nrCopies+1) - 1)
    For nrCopies = 0 To nrCopies - 1
        For UB = UB + 1 To UB + lCount 
            StringArray(UB) = StringArray(LB)
            LB = LB + 1
        Next
        LB = LB - lCount
    Next
    It no copy all data

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    16,500

    Re: Duplicate array using copymemory

    That was air-code and appears I forgot to account for something...

    After LB = LB - lCount, add this: UB = UB - 1
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VB and DPI Tutorial} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    673

    Re: Duplicate array using copymemory

    Quote Originally Posted by LaVolpe View Post
    That was air-code and appears I forgot to account for something...

    After LB = LB - lCount, add this: UB = UB - 1
    return error
    Subscript out of range

    in second interation

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    16,500

    Re: Duplicate array using copymemory

    I don't have VB on this machine, so I can't troubleshoot it for you.
    Just walk it thru and figure out where my math went wrong. In my head, looks like it should work.

    Edited: Did follow up with a personal test when I got home. It worked as expected? Did you get it working for you
    Last edited by LaVolpe; Mar 19th, 2015 at 08:02 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VB and DPI Tutorial} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.