0

I'm not familiar with CopyMemory but would like to figure out how to calculate the length to copy a whole array. Your helps is very grateful.

assuming there is a 2D array which value type includes string ,integer, single etc. The code as below:

Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) 

Sub Test4()
    Dim arr(1 To 3, 1 To 2) 
    arr(1, 1) = "abc"
    arr(2, 1) = "befgh"
    arr(3, 1) = 1
    arr(1, 2) = 468888
    arr(2, 2) = 999.8
    arr(3, 2) = "ijklmnopq"
    
    Dim brr(1 To 3, 1 To 2)
    CopyMemory VarPtr(brr(1, 1)), VarPtr(arr(1, 1)), 16 * Ubound(arr) * Ubound(arr,2)
End Sub

The length doesn't not cover the range of arr, and just copy some value but not all . I wonder how to calculate the length to copy all value .

braX
  • 11,506
  • 5
  • 20
  • 33
Raylene
  • 13
  • 4
  • 2
    I suggest to not use low level memory access functions if yous don't understand exactly how the memory is allocated. Here you have a 2D array of variants, how do you expect the data to be stored? – Vincent G Jul 29 '21 at 06:45
  • Find the API explained here: http://www.jasinskionline.com/windowsapi/ref/c/copymemory.html. The critical part of that link is:- "CopyMemory, as far as the Windows API is concerned, is perfectly identical to the MoveMemory function; in fact, they actually are the same function! CopyMemory moves the contents of a portion of memory from one location to another. The two locations are identified by pointers to the memory addresses. After the copy, the original contents in the source are set to zeros." What are you trying to do? There should be better ways. – Variatus Jul 29 '21 at 07:48
  • Copying an array of 1000 * 10000 (thats 10 million members) takes a fraction of a second using 2 nested loops - why use memCpy? I don't know the exact internal implementation of strings, but the array itself contains for sure only a pointer or reference, and the real string is not copied. Same for objects – FunThomas Jul 29 '21 at 08:07
  • @ FunThomas ,if you ask me why not use nested loops to copy , i am just curious of usage of copymemory .Thanks for the explanation .The array itself has pointer which contains another pointers(address to storage the real string in memory ) for that strings . I get it. – Raylene Jul 30 '21 at 06:16
  • @Variatus.Thanks for the documentation. what i am trying to do is if any another way to copy array like slice array .Actually , i want some portion of array instead of all and i used loop to achieve it .But recently , i found this copymemory function which is easy to copy ( i think)and tried to make it copied . But due to lack of related knowledge , so i ask you for help . – Raylene Jul 30 '21 at 06:30
  • @Vincent G.Yeah , you're right .maybe i shouldn't use it and i don't know exactly how th d function working. What do you mean by 'how do you expect the data to be stored?' I just want the array(brr) has same data as arr by using that function. – Raylene Jul 30 '21 at 06:47

0 Answers0