-
Aug 8th, 2022, 08:13 AM
#1
Thread Starter
New Member
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)?
-
Aug 8th, 2022, 08:33 AM
#2
Re: ReDim of a Shallow copy of a jagged array
Trying to be specific here. Are you looking for a b(j)?
https://github.com/yereverluvinunclebert
Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.
By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.
-
Aug 8th, 2022, 09:31 AM
#3
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
-
Aug 8th, 2022, 09:31 AM
#4
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
-
Aug 8th, 2022, 09:33 AM
#5
Re: ReDim of a Shallow copy of a jagged array
Originally Posted by techgnome
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
-
Aug 8th, 2022, 10:07 AM
#6
Thread Starter
New Member
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.
Then
Code:
console.WriteLine( "Value of a: {0}", a(1)(1))
Would show
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)
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.
-
Aug 8th, 2022, 11:13 AM
#7
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
-
Aug 8th, 2022, 12:43 PM
#8
Thread Starter
New Member
Re: ReDim of a Shallow copy of a jagged array
Originally Posted by Shaggy Hiker
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.
-
Aug 8th, 2022, 02:30 PM
#9
Re: ReDim of a Shallow copy of a jagged array
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|