Arrays and CopyMemory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mikael Lundberg

    Arrays and CopyMemory

    Hi!

    Just one question

    Is it legal to copy the array descriptor in this way :

    Dim lngArr1() as Long
    Dim lngArr2() as Long
    Redim lngArr1(2) as Long

    CopyMemory ByVal VarPtrArray(lng Arr2), ByVal VarPtrArray(lng Arr1), 4

    Or can you do a shallow-copy of an array in some other way?

    /Mikael Lundberg
  • River Gorge

    #2
    Re: Arrays and CopyMemory

    On 20 Feb 2004 06:55:04 -0800, Mikael Lundberg <mikael.lundber g@home.se>
    wrote:
    [color=blue]
    > Hi!
    >
    > Just one question
    >
    > Is it legal to copy the array descriptor in this way :
    >
    > Dim lngArr1() as Long
    > Dim lngArr2() as Long
    > Redim lngArr1(2) as Long
    >
    > CopyMemory ByVal VarPtrArray(lng Arr2), ByVal VarPtrArray(lng Arr1), 4
    >
    > Or can you do a shallow-copy of an array in some other way?
    >
    > /Mikael Lundberg[/color]

    Your code looks like it is supposed to copy the pointer of one array to
    another.
    This fools VB into having both array variables point to the same memory
    location. This is not leagal, but it works on all versions of Windows.
    However, the code will crash if you don't use CopyMemory to clear lngArr2
    before it goes out of scope.

    I'm not sure what you mean by "shallow-Copy".

    There is virtually never a reason to do this: The net effect of copying
    the pointer of lngArr1 to lngArr2, then changing values in lngArr2, is
    changing values in lngArr1. The only reason to do this would be if you
    didn't have access to lngArr1, like when lngArr1 is in a Class and you
    want to change it from outside the Class.

    Comment

    Working...