Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Array of Double - Shallow vs. Deep Cloning?

Array of Double - Shallow vs. Deep Cloning?

Scheduled Pinned Locked Moved .NET (Core and Framework)
visual-studiodata-structuresquestion
11 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    M Badger
    wrote on last edited by
    #1

    So I'm quite comfortable with shallow and deep cloning for value and reference types but want to check how this plays out for an array of a value type. I have a 2D array of double and since the array contains value types I think the Array.Clone method (whilst only guaranteeing shallow copies) will give a deep copy in this instance. Am I right?

    Private _matrix as double(,)

    Public Function CopyToArray() as Double(,)
    Return Me._matrix.Clone()
    End Fucntion

    Public Function Copy() As Matrix
    Return New Matrix(Me.CopyToArray())
    End Function

    Public Sub New(ByVal array As Double(,))
    Me._matrix = array
    End Sub

    The constructor assumes that the user wants to assign the 2D array of double(,) they have already created to the new matrix instance and hence doesn't use Array.Clone (and since the array is a class and is therefore a reference type, ByVal essentially has no meaning in this context). Did I get that bit right? Thanks, Mike

    L 1 Reply Last reply
    0
    • M M Badger

      So I'm quite comfortable with shallow and deep cloning for value and reference types but want to check how this plays out for an array of a value type. I have a 2D array of double and since the array contains value types I think the Array.Clone method (whilst only guaranteeing shallow copies) will give a deep copy in this instance. Am I right?

      Private _matrix as double(,)

      Public Function CopyToArray() as Double(,)
      Return Me._matrix.Clone()
      End Fucntion

      Public Function Copy() As Matrix
      Return New Matrix(Me.CopyToArray())
      End Function

      Public Sub New(ByVal array As Double(,))
      Me._matrix = array
      End Sub

      The constructor assumes that the user wants to assign the 2D array of double(,) they have already created to the new matrix instance and hence doesn't use Array.Clone (and since the array is a class and is therefore a reference type, ByVal essentially has no meaning in this context). Did I get that bit right? Thanks, Mike

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      It will copy the values. Now tell me, is that shallow or deep? :)

      Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

      M 1 Reply Last reply
      0
      • L Lost User

        It will copy the values. Now tell me, is that shallow or deep? :)

        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

        M Offline
        M Offline
        M Badger
        wrote on last edited by
        #3

        Eddy Vluggen wrote:

        Now tell me, is that shallow or deep?

        :) Taking the question seiously for a moment, for copy/clone of value types or arrays of a value type, shallow == deep. For reference types, copy/clone shallow != deep. So, last question, since an array is a reference type then there's a vast difference between these two alternatives:

        Private _matrix as double(,)
        Public Sub New(ByVal array As Double(,))
        Me._matrix = array
        End Sub

        and

        Private _matrix as double(,)
        Public Sub New(ByVal array As Double(,))
        Me._matrix = array.Clone
        End Sub

        Whereas with an array of reference types there would only be a subtle difference? Mike

        L 1 Reply Last reply
        0
        • M M Badger

          Eddy Vluggen wrote:

          Now tell me, is that shallow or deep?

          :) Taking the question seiously for a moment, for copy/clone of value types or arrays of a value type, shallow == deep. For reference types, copy/clone shallow != deep. So, last question, since an array is a reference type then there's a vast difference between these two alternatives:

          Private _matrix as double(,)
          Public Sub New(ByVal array As Double(,))
          Me._matrix = array
          End Sub

          and

          Private _matrix as double(,)
          Public Sub New(ByVal array As Double(,))
          Me._matrix = array.Clone
          End Sub

          Whereas with an array of reference types there would only be a subtle difference? Mike

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Mike-MadBadger wrote:

          for copy/clone of value types or arrays of a value type, shallow == deep

          :thumbsup:

          Mike-MadBadger wrote:

          Private _matrix as double(,)
          Public Sub New(ByVal array As Double(,))
          Me._matrix = array
          End Sub

          Would assign the pointer to the array to "Me._matrix". (Both in case of value- and reference types)

          Mike-MadBadger wrote:

          Private _matrix as double(,)
          Public Sub New(ByVal array As Double(,))
          Me._matrix = array.Clone
          End Sub

          Would create a copy of the values (shallow, again for value and reference-types)

          Mike-MadBadger wrote:

          Whereas with an array of reference types there would only be a subtle difference?

          In the case of value-types, the result would be identical; you'd end up with an array of values. For reference-types, it'd be similar; version one assigns a pointer to the array, version two copies the structure of the array (and thus, all pointers to references it contains) and you end up with an exact copy of the list, still pointing to the original objects.

          MSDN states[^]

          A shallow copy of an Array copies only the elements of the Array, whether they are reference types or value types, but it does not copy the objects that the references refer to. The references in the new Array point to the same objects that the references in the original Array point to.

          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

          Z M 2 Replies Last reply
          0
          • L Lost User

            Mike-MadBadger wrote:

            for copy/clone of value types or arrays of a value type, shallow == deep

            :thumbsup:

            Mike-MadBadger wrote:

            Private _matrix as double(,)
            Public Sub New(ByVal array As Double(,))
            Me._matrix = array
            End Sub

            Would assign the pointer to the array to "Me._matrix". (Both in case of value- and reference types)

            Mike-MadBadger wrote:

            Private _matrix as double(,)
            Public Sub New(ByVal array As Double(,))
            Me._matrix = array.Clone
            End Sub

            Would create a copy of the values (shallow, again for value and reference-types)

            Mike-MadBadger wrote:

            Whereas with an array of reference types there would only be a subtle difference?

            In the case of value-types, the result would be identical; you'd end up with an array of values. For reference-types, it'd be similar; version one assigns a pointer to the array, version two copies the structure of the array (and thus, all pointers to references it contains) and you end up with an exact copy of the list, still pointing to the original objects.

            MSDN states[^]

            A shallow copy of an Array copies only the elements of the Array, whether they are reference types or value types, but it does not copy the objects that the references refer to. The references in the new Array point to the same objects that the references in the original Array point to.

            Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

            Z Offline
            Z Offline
            Zeeshan Ahmed Memon
            wrote on last edited by
            #5

            Thanks

            1 Reply Last reply
            0
            • L Lost User

              Mike-MadBadger wrote:

              for copy/clone of value types or arrays of a value type, shallow == deep

              :thumbsup:

              Mike-MadBadger wrote:

              Private _matrix as double(,)
              Public Sub New(ByVal array As Double(,))
              Me._matrix = array
              End Sub

              Would assign the pointer to the array to "Me._matrix". (Both in case of value- and reference types)

              Mike-MadBadger wrote:

              Private _matrix as double(,)
              Public Sub New(ByVal array As Double(,))
              Me._matrix = array.Clone
              End Sub

              Would create a copy of the values (shallow, again for value and reference-types)

              Mike-MadBadger wrote:

              Whereas with an array of reference types there would only be a subtle difference?

              In the case of value-types, the result would be identical; you'd end up with an array of values. For reference-types, it'd be similar; version one assigns a pointer to the array, version two copies the structure of the array (and thus, all pointers to references it contains) and you end up with an exact copy of the list, still pointing to the original objects.

              MSDN states[^]

              A shallow copy of an Array copies only the elements of the Array, whether they are reference types or value types, but it does not copy the objects that the references refer to. The references in the new Array point to the same objects that the references in the original Array point to.

              Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

              M Offline
              M Offline
              M Badger
              wrote on last edited by
              #6

              Apologies for the delay, been busy leaving work ready to move to a new country !! I think we are absolutely agreeing but saying it in different ways, so for clarity I wanted to check this comment:

              Eddy Vluggen wrote:

              In the case of value-types, the result would be identical; you'd end up with an array of values.

              I don't understand the identical bit. In the first case you have a pointer to an existing instance of an array in the second case you create a new instance of an array with the same structure as the one you cloned? Perhaps pedantry but in this case, at least for understanding, forgiveable pedantry I feel... That's what leads to clarity over the value / reference difference. In the value case the new array structure contains new, independent value 'objects'. Whereas in the reference case the new array structure contains new, independent pointers to the same object instances that the pointers in the original array pointed to. Mike

              L 1 Reply Last reply
              0
              • M M Badger

                Apologies for the delay, been busy leaving work ready to move to a new country !! I think we are absolutely agreeing but saying it in different ways, so for clarity I wanted to check this comment:

                Eddy Vluggen wrote:

                In the case of value-types, the result would be identical; you'd end up with an array of values.

                I don't understand the identical bit. In the first case you have a pointer to an existing instance of an array in the second case you create a new instance of an array with the same structure as the one you cloned? Perhaps pedantry but in this case, at least for understanding, forgiveable pedantry I feel... That's what leads to clarity over the value / reference difference. In the value case the new array structure contains new, independent value 'objects'. Whereas in the reference case the new array structure contains new, independent pointers to the same object instances that the pointers in the original array pointed to. Mike

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Mike-MadBadger wrote:

                been busy leaving work ready to move to a new country !!

                Getting ready for adventure? From where to where?

                Mike-MadBadger wrote:

                I don't understand the identical bit. [..] Perhaps pedantry but in this case, at least for understanding, forgiveable pedantry I feel...

                It's not pedantry, but being precise.

                Mike-MadBadger wrote:

                In the first case you have a pointer to an existing instance of an array in the second case you create a new instance of an array with the same structure as the one you cloned?

                Exactly :thumbsup:

                Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                M D 2 Replies Last reply
                0
                • L Lost User

                  Mike-MadBadger wrote:

                  been busy leaving work ready to move to a new country !!

                  Getting ready for adventure? From where to where?

                  Mike-MadBadger wrote:

                  I don't understand the identical bit. [..] Perhaps pedantry but in this case, at least for understanding, forgiveable pedantry I feel...

                  It's not pedantry, but being precise.

                  Mike-MadBadger wrote:

                  In the first case you have a pointer to an existing instance of an array in the second case you create a new instance of an array with the same structure as the one you cloned?

                  Exactly :thumbsup:

                  Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                  M Offline
                  M Offline
                  M Badger
                  wrote on last edited by
                  #8

                  Eddy Vluggen wrote:

                  Getting ready for adventure? From where to where?

                  UK (Reading) to France (Lyon). Same industry, different company, family et al in tow. It certainly will be an adventure. Thanks for asking.

                  L 1 Reply Last reply
                  0
                  • L Lost User

                    Mike-MadBadger wrote:

                    been busy leaving work ready to move to a new country !!

                    Getting ready for adventure? From where to where?

                    Mike-MadBadger wrote:

                    I don't understand the identical bit. [..] Perhaps pedantry but in this case, at least for understanding, forgiveable pedantry I feel...

                    It's not pedantry, but being precise.

                    Mike-MadBadger wrote:

                    In the first case you have a pointer to an existing instance of an array in the second case you create a new instance of an array with the same structure as the one you cloned?

                    Exactly :thumbsup:

                    Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                    D Offline
                    D Offline
                    dojohansen
                    wrote on last edited by
                    #9

                    As to whether that difference is *important*... well, that completely depends on what you do with them. Say you have a User object and this involves the concept of a list of Favourites (products, girls, doesn't matter). It'd probably be a List<Favourite> rather than a Favourite[], but no matter. The point is if for some reason you wanted to let one user copy another user's favourites, what should you do? Just point to the same list? Or make an actual copy of the list? Surely you should copy it, that way if user A subsequently edits his copy, the other one is not affected. What about the Product objects themselves? Well, there's no need to clone them unless they "belong" to the user. If they are shared, you could happily point to the same Products, and both users would see any changes to products they have in their respective lists. -- I know this isn't the right forum for it, but I gotta ask: Are you really from Hell, Eddy? A lot of English speakers claim they're from Hell, but most of them don't even know where it is! Look up "Hell, Stjørdal, Norway" on Google Maps, and you won't be one of them. ;)

                    It depends. It *always* depends.

                    L 1 Reply Last reply
                    0
                    • M M Badger

                      Eddy Vluggen wrote:

                      Getting ready for adventure? From where to where?

                      UK (Reading) to France (Lyon). Same industry, different company, family et al in tow. It certainly will be an adventure. Thanks for asking.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Good luck and enjoy :cool:

                      1 Reply Last reply
                      0
                      • D dojohansen

                        As to whether that difference is *important*... well, that completely depends on what you do with them. Say you have a User object and this involves the concept of a list of Favourites (products, girls, doesn't matter). It'd probably be a List<Favourite> rather than a Favourite[], but no matter. The point is if for some reason you wanted to let one user copy another user's favourites, what should you do? Just point to the same list? Or make an actual copy of the list? Surely you should copy it, that way if user A subsequently edits his copy, the other one is not affected. What about the Product objects themselves? Well, there's no need to clone them unless they "belong" to the user. If they are shared, you could happily point to the same Products, and both users would see any changes to products they have in their respective lists. -- I know this isn't the right forum for it, but I gotta ask: Are you really from Hell, Eddy? A lot of English speakers claim they're from Hell, but most of them don't even know where it is! Look up "Hell, Stjørdal, Norway" on Google Maps, and you won't be one of them. ;)

                        It depends. It *always* depends.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        dojohansen wrote:

                        The point is if for some reason you wanted to let one user copy another user's favourites, what should you do? Just point to the same list?

                        I'd serialize the list, as the user won't be sharing my PC's memory :cool: The most obvious route would be XML.

                        dojohansen wrote:

                        I know this isn't the right forum for it, but I gotta ask: Are you really from Hell, Eddy?

                        It *depends*; if you mean the physical Hell from Norway, I hope to spend my old age there. Not only is it a very low-populated area, one also gets a 6-month night! I heard that one can even get there by train, and internet can probably be arranged too :)

                        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups