Results 1 to 9 of 9

Thread: ReDim of a Shallow copy of a jagged array

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2022
    Posts
    3

    ReDim of a Shallow copy of a jagged array

    Hello,

    Could anyone help me out and explain the exact behaviour of a ReDim on shallow copies - to illustrate I've the following example:
    Code:
     
    Const dim1 = 100
    Const dim2 = 200
    
    Public a as Double()()
    Public b as Double()()
    
    ReDim a(dim1)
    
    For j = 0 to dim1 
       ReDim a(j)(dim2)
    Next
    
    'Assign some values to all entries in a'
    
    ReDim b(dim1)
    a.CopyTo(b,0)
    At this step each of the elements of b should be a shallow copy of the elements of a - am I correct?

    Then proceeding:
    Code:
    For j = 0 to dim 1
       ReDim b(j)(dim2)
    Next
    Executing a similar code in my example, changes to values of b(j) won't affect a(j), so by a ReDim, the shallow copy has been turned into a deep copy?
    Furthermore, would I be required to use ReDim Preserve (the sizes of each of the arrays are fixed prior in my usecase)?

  2. #2
    Frenzied Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    1,596

    Re: ReDim of a Shallow copy of a jagged array

    Trying to be specific here. Are you looking for a b(j)?
    By the power invested in me, all the threads I start are Niya and Olaf free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,927

    Re: ReDim of a Shallow copy of a jagged array

    Doubles don't have pointers ... so when you copy a to b, it's copying the values, not pointers. So changes to b or a will not affect the other. Now if a was full of pointers to an object of some kind, then you would be copying the pointer over (since that's what's in a) to b. And then, yes changes to b would be reflected in a. But as it is right now, you're copying what's in a over to b, and that is a primitive double, so you get the value to it.

    There may be a way to copy a to b, that grabs the pointer to the values, but I don't think it's quite that simple.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    37,579

    Re: ReDim of a Shallow copy of a jagged array

    You aren't going to turn a shallow copy into a deep copy by any means.

    Considering that the array is an array of double, what is the distinction you are making between shallow and deep copies? Doubles are value types. When you copy a double from A to B, in your example, there is really no difference between shallow and deep. Arrays, however, are not value types. If you copy an array from A to B, then you are copying a reference to the array. Therefore, in your first snippet, each element of B is an array that is a shallow copy of the elements of A.

    What you do in the second snippet is to create a new array for each element of B. Those new arrays are empty, so they aren't a copy of anything, shallow or deep. They're just new objects.

    Redim creates a new array of the new size. Essentially, it allocates some new memory for the array. That memory holds nothing.
    Redim Preserve creates a new array of the new size, then, assuming there is something in the old array, it copies the old over to the new. The old, in this case, holds a reference to an array. That reference will get copied over to the new slots. That won't magically become a deep copy.
    My usual boring signature: Nothing

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    37,579

    Re: ReDim of a Shallow copy of a jagged array

    Quote Originally Posted by techgnome View Post
    Doubles don't have pointers ... so when you copy a to b, it's copying the values, not pointers. So changes to b or a will not affect the other. Now if a was full of pointers to an object of some kind, then you would be copying the pointer over (since that's what's in a) to b. And then, yes changes to b would be reflected in a. But as it is right now, you're copying what's in a over to b, and that is a primitive double, so you get the value to it.

    There may be a way to copy a to b, that grabs the pointer to the values, but I don't think it's quite that simple.


    -tg
    Actually, what's in A is not a double, it's an array of double.

    2D arrays make very little sense in .NET. Redim makes only a little sense in .NET, and Redim Preserve makes no sense. If you are using Redim Preserve, then you should have been using a List rather than an array.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2022
    Posts
    3

    Re: ReDim of a Shallow copy of a jagged array

    Thanks for the answers so far. Sorry if my vocab is a bit off, I'm somewhat new to programming.
    I'll try to clarify a bit more:
    Code:
    ReDim b(dim1)
    a.CopyTo(b,0)
    After this point of code, when I assign a new value to a specific element of b (to be precise, of b(1)) , e.g.
    Code:
    b(1)(1) = 5
    Then

    Code:
    console.WriteLine( "Value of a: {0}", a(1)(1))
    Would show
    Code:
    Value of a: 5
    On the other hand, after a ReDim
    Code:
    For j = 0 to dim 1
       ReDim b(j)(dim2)
    Next
    The same output would yield (assuming all entries of a to be initialized to 0)
    Code:
    Value of a: 0
    What I'd like to know is, what the ReDim is actually doing? At first the CopyTo would copy the references of each array in a to b, and the ReDim executed on each array in b - each of which is composed of doubles - would create a deep copy, as doubles are immutables? The ReDim definitely doesn't get rid of any information in each element of b.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    37,579

    Re: ReDim of a Shallow copy of a jagged array

    Why are all values of a initialized to 0? You started off by saying that a(1)(1) was 5. What is the code that made a(1)(1) 0?

    What ReDim should be doing is creating a new array of the new size, and that's all it should be doing. There's no deep copy, and it has nothing to do with doubles being immutable. Copy copies the contents of the array. If the array holds a double, then it copies that double. If the array holds a reference, then Copy copies the reference (which is essentially just the memory address of the actual object). It's doing the same thing in all cases.

    ReDim Preserve shouldn't get rid of any information in b, since that's what Preserve is about, but ReDim should.

    So, it seems like something is missing, but I'm not sure quite what. Have you actually tried this, or are you just thinking it through?
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2022
    Posts
    3

    Re: ReDim of a Shallow copy of a jagged array

    Quote Originally Posted by Shaggy Hiker View Post
    Why are all values of a initialized to 0? You started off by saying that a(1)(1) was 5. What is the code that made a(1)(1) 0?
    That's what I meant by this line:
    Code:
    'Assign some values to all entries in a'
    Just initialize them to something, as for the reminder it doesn't matter what it is. For simplicity I assumed that it has been assinged zero in my second post (so assignign b(1)(1) =5 would also change a(1)(1) in the example)

    What ReDim should be doing is creating a new array of the new size, and that's all it should be doing.
    That's what I though it should be doing, but for some reason when executed it keeps the original values.

    There's no deep copy, and it has nothing to do with doubles being immutable. Copy copies the contents of the array. If the array holds a double, then it copies that double. If the array holds a reference, then Copy copies the reference (which is essentially just the memory address of the actual object). It's doing the same thing in all cases.

    ReDim Preserve shouldn't get rid of any information in b, since that's what Preserve is about, but ReDim should.

    Have you actually tried this, or are you just thinking it through?
    Not exactly the code fragments, but embedded in a bigger project. I've been tracing the values assigned to the array, and in the case without any ReDim on b - b only contains references to arrays in this case (If I understand correctly) - it alters the values when displaying a whenever I change them on b (as I'd expect it to do). On the other hand, a ReDim (without the Preserve option)
    on the individual entries of b (i.e. on every array of double), would keep the values assigned to each element of an array in a (e.g. a(1)(1) = 0) whenever any element of an element of b is updated. Here the ReDim on an array in b would "destroy" the references to the corresponding memory adress (that of the array in a), thus it no longer affecting a is expected.
    What I don't see is why it would copy the values onto the new allocated memory.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    37,579

    Re: ReDim of a Shallow copy of a jagged array

    I wouldn't expect it to.
    My usual boring signature: Nothing

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