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. C / C++ / MFC
  4. what is deep copy and shallow copy??

what is deep copy and shallow copy??

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
10 Posts 7 Posters 0 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.
  • V Offline
    V Offline
    voorugonda prashanth
    wrote on last edited by
    #1

    please explain in detail. if possible give any example.

    S M G 3 Replies Last reply
    0
    • V voorugonda prashanth

      please explain in detail. if possible give any example.

      S Offline
      S Offline
      Sarath C
      wrote on last edited by
      #2

      // shallow copy Fxn(Class1 Obj1) { m_pPointer = Obj1.m_pPinter; // just copying the where the pointer points to } // Deepcopy Fxn(Class1 Obj1) { m_pPinter = new int[obj1.size] memcpy(m_pPointer,Obj1.m_pPinter,Obj1.size); // Allocating memory and copying its contents } -Sarath

      1 Reply Last reply
      0
      • V voorugonda prashanth

        please explain in detail. if possible give any example.

        M Offline
        M Offline
        Maxwell Chen
        wrote on last edited by
        #3

        C++ Notes: Shallow V.S. Deep Copies[^].


        Maxwell Chen

        1 Reply Last reply
        0
        • V voorugonda prashanth

          please explain in detail. if possible give any example.

          G Offline
          G Offline
          Ganesh_T
          wrote on last edited by
          #4

          A shallow copy of an object copies all of the member field values. This works well if the fields are values, but may not be what you want for fields that point to dynamically allocated memory. The pointer will be copied. but the memory it points to will not be copied -- the field in both the original object and the copy will then point to the same dynamically allocated memory, which is not usually what you want. The default copy constructor and assignment operator make shallow copies. A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. To make a deep copy, you must write a copy constructor and overload the assignment operator. Deep copy needs, If an object has pointers to dynamically allocated memory, and the dynamically allocated memory needs to be copied when the original object is copied, then a deep copy is required. A class that requires deep copies will generally need: i )a destructor to delete the dynamically allocated memory. ii) a copy constructor to make a copy of the dynamically allocated memory. iii) an overloaded assignment operator to make a copy of the dynamically allocated memory. Cheers "Peace of mind through Technology"

          M 1 Reply Last reply
          0
          • G Ganesh_T

            A shallow copy of an object copies all of the member field values. This works well if the fields are values, but may not be what you want for fields that point to dynamically allocated memory. The pointer will be copied. but the memory it points to will not be copied -- the field in both the original object and the copy will then point to the same dynamically allocated memory, which is not usually what you want. The default copy constructor and assignment operator make shallow copies. A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. To make a deep copy, you must write a copy constructor and overload the assignment operator. Deep copy needs, If an object has pointers to dynamically allocated memory, and the dynamically allocated memory needs to be copied when the original object is copied, then a deep copy is required. A class that requires deep copies will generally need: i )a destructor to delete the dynamically allocated memory. ii) a copy constructor to make a copy of the dynamically allocated memory. iii) an overloaded assignment operator to make a copy of the dynamically allocated memory. Cheers "Peace of mind through Technology"

            M Offline
            M Offline
            Maxwell Chen
            wrote on last edited by
            #5

            Ganesh_T wrote:

            A shallow copy of an object ...

            Exactly the same as the link I found. :-D


            Maxwell Chen

            N 1 Reply Last reply
            0
            • M Maxwell Chen

              Ganesh_T wrote:

              A shallow copy of an object ...

              Exactly the same as the link I found. :-D


              Maxwell Chen

              N Offline
              N Offline
              Nibu babu thomas
              wrote on last edited by
              #6

              Is it a Copy/Paste? :suss::^)


              Nibu thomas Software Developer Faqs by Michael dunn

              M 1 Reply Last reply
              0
              • N Nibu babu thomas

                Is it a Copy/Paste? :suss::^)


                Nibu thomas Software Developer Faqs by Michael dunn

                M Offline
                M Offline
                Maxwell Chen
                wrote on last edited by
                #7

                Nibu thomas wrote:

                Is it a Copy/Paste?

                It's a deep copy! :-D


                Maxwell Chen

                N S T 3 Replies Last reply
                0
                • M Maxwell Chen

                  Nibu thomas wrote:

                  Is it a Copy/Paste?

                  It's a deep copy! :-D


                  Maxwell Chen

                  N Offline
                  N Offline
                  Nibu babu thomas
                  wrote on last edited by
                  #8

                  ROTFL.... :laugh::laugh::laugh::laugh:

                  Maxwell Chen wrote:

                  It's a deep copy!

                  How deep...:-D


                  Nibu thomas Software Developer Faqs by Michael dunn

                  1 Reply Last reply
                  0
                  • M Maxwell Chen

                    Nibu thomas wrote:

                    Is it a Copy/Paste?

                    It's a deep copy! :-D


                    Maxwell Chen

                    S Offline
                    S Offline
                    Sebastian Schneider
                    wrote on last edited by
                    #9

                    ROTFL. OMG... Im a geek. I actually a) got the joke b) found it insanely funny. Well... I can live with that :D

                    1 Reply Last reply
                    0
                    • M Maxwell Chen

                      Nibu thomas wrote:

                      Is it a Copy/Paste?

                      It's a deep copy! :-D


                      Maxwell Chen

                      T Offline
                      T Offline
                      ThatsAlok
                      wrote on last edited by
                      #10

                      Maxwell Chen wrote:

                      It's a deep copy! :-D

                      :)

                      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                      cheers, Alok Gupta VC Forum Q&A :- I/ IV

                      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