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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. deep/ shallow copy

deep/ shallow copy

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
7 Posts 4 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.
  • K Offline
    K Offline
    kumar_mk
    wrote on last edited by
    #1

    please give detailed differences between deep copy and shallow copy (also in terms of memory allocation)? which ( userdefined or default copy --- constructor/ overloaded assignment operator) performs what ( deep/ shallow copy)? thanx in advance kumar_windows

    T 1 Reply Last reply
    0
    • K kumar_mk

      please give detailed differences between deep copy and shallow copy (also in terms of memory allocation)? which ( userdefined or default copy --- constructor/ overloaded assignment operator) performs what ( deep/ shallow copy)? thanx in advance kumar_windows

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      kumar_windows wrote:

      which ( userdefined or default copy --- constructor/ overloaded assignment operator) performs what ( deep/ shallow copy)?

      depends how they've been implemented. i don't know what is a deep copy nor shallow copy, but a good copy operation should duplicate each data owned by the object copied (whatever copy conctructor or operator= ).


      TOXCCT >>> GEII power

      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

      L 1 Reply Last reply
      0
      • T toxcct

        kumar_windows wrote:

        which ( userdefined or default copy --- constructor/ overloaded assignment operator) performs what ( deep/ shallow copy)?

        depends how they've been implemented. i don't know what is a deep copy nor shallow copy, but a good copy operation should duplicate each data owned by the object copied (whatever copy conctructor or operator= ).


        TOXCCT >>> GEII power

        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        toxcct wrote:

        i don't know what is a deep copy nor shallow copy

        In C++ a common use of deep vs. shallow is in reference to pointer members of classes. A deep copy will allocate it's own memory and copy the data. A shallow copy will only copy the pointer (address).

        "What classes are you using ? You shouldn't call stuff if you have no idea what it does"
        Christian Graus in the C# forum

        led mike

        T 1 Reply Last reply
        0
        • L led mike

          toxcct wrote:

          i don't know what is a deep copy nor shallow copy

          In C++ a common use of deep vs. shallow is in reference to pointer members of classes. A deep copy will allocate it's own memory and copy the data. A shallow copy will only copy the pointer (address).

          "What classes are you using ? You shouldn't call stuff if you have no idea what it does"
          Christian Graus in the C# forum

          led mike

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          in general, it is bad to copy only the addresses (pointers contents) because when one object desctructor is freed, it will invalidate the other object which points on that address...


          TOXCCT >>> GEII power

          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

          L 1 Reply Last reply
          0
          • T toxcct

            in general, it is bad to copy only the addresses (pointers contents) because when one object desctructor is freed, it will invalidate the other object which points on that address...


            TOXCCT >>> GEII power

            [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            toxcct wrote:

            in general, it is bad to copy only the addresses

            Well you better get busy telling the rest of the industry that because there is a common design pattern known as "copy on write" that calls for doing just that. At one time, maybe still... i don't know, CString used that design.

            "What classes are you using ? You shouldn't call stuff if you have no idea what it does"
            Christian Graus in the C# forum

            led mike

            Z 1 Reply Last reply
            0
            • L led mike

              toxcct wrote:

              in general, it is bad to copy only the addresses

              Well you better get busy telling the rest of the industry that because there is a common design pattern known as "copy on write" that calls for doing just that. At one time, maybe still... i don't know, CString used that design.

              "What classes are you using ? You shouldn't call stuff if you have no idea what it does"
              Christian Graus in the C# forum

              led mike

              Z Offline
              Z Offline
              Zac Howland
              wrote on last edited by
              #6

              led mike wrote:

              Well you better get busy telling the rest of the industry that because there is a common design pattern known as "copy on write" that calls for doing just that.

              Copy-on-write creates a copy of the resource when you try to change it. Most of the time it is recommended to use a reference count when using COW to prevent both excessive copies and invalid memory accesses. The post you were responding to was correct, IN GENERAL, it is a bad idea to simply copy the addresses. There are special cases and techniques that allow for doing such without problems, but they are special cases, not the general case.

              led mike wrote:

              At one time, maybe still... i don't know, CString used that design.

              Not CString, the STL string class does this. CString has always had a private copy of the data (when you copy a CString, it actually copies the bytes to a new buffer). STL-string uses a reference count on the buffer and only creates a private buffer if you try to change it. This is also the reason that it is recommended to use vector if you are dealing with passing strings between threads. To answer the original post more thoroughly, a shallow copy is what is done for you if no copy constructor nor copy-assignment operators are declared. Basically, pointers in your objects are copied (that is, not the data they point to). A deep copy is something you have to write yourself by implementing the copy-constructor/assignment operator to actually allocate its own memory and copy the bytes from the source to the destination (that is, your new object). The following code segments are examples: class Shallow { private: char* m_MyString; public: Shallow() { m_MyString = new char[20]; } ~Shallow() { delete [] m_MyString; } }; class Deep { private: char* m_MyString; public: Deep() { m_MyString = new char[20]; } Deep(const Deep& d) { memcpy(m_MyString, d.m_MyString); } ~Deep() { delete [] m_MyString; } const Deep& operator= (const Deep& d) { memcpy(m_MyString, d.m_MyString); } }; void main() { Shallow a, b; Deep c, d; // shallow copy a = b; // this will cause a.m_MyString == b.m_MyString // this will also cause an error when the objects go out of scope // since the pointer will be deleted twice! // deep copy c = d; // this will cause c.m_MyString != d.m_MyString // however, strcpy(c.m_MyString, d.m_MyString) == 0

              L 1 Reply Last reply
              0
              • Z Zac Howland

                led mike wrote:

                Well you better get busy telling the rest of the industry that because there is a common design pattern known as "copy on write" that calls for doing just that.

                Copy-on-write creates a copy of the resource when you try to change it. Most of the time it is recommended to use a reference count when using COW to prevent both excessive copies and invalid memory accesses. The post you were responding to was correct, IN GENERAL, it is a bad idea to simply copy the addresses. There are special cases and techniques that allow for doing such without problems, but they are special cases, not the general case.

                led mike wrote:

                At one time, maybe still... i don't know, CString used that design.

                Not CString, the STL string class does this. CString has always had a private copy of the data (when you copy a CString, it actually copies the bytes to a new buffer). STL-string uses a reference count on the buffer and only creates a private buffer if you try to change it. This is also the reason that it is recommended to use vector if you are dealing with passing strings between threads. To answer the original post more thoroughly, a shallow copy is what is done for you if no copy constructor nor copy-assignment operators are declared. Basically, pointers in your objects are copied (that is, not the data they point to). A deep copy is something you have to write yourself by implementing the copy-constructor/assignment operator to actually allocate its own memory and copy the bytes from the source to the destination (that is, your new object). The following code segments are examples: class Shallow { private: char* m_MyString; public: Shallow() { m_MyString = new char[20]; } ~Shallow() { delete [] m_MyString; } }; class Deep { private: char* m_MyString; public: Deep() { m_MyString = new char[20]; } Deep(const Deep& d) { memcpy(m_MyString, d.m_MyString); } ~Deep() { delete [] m_MyString; } const Deep& operator= (const Deep& d) { memcpy(m_MyString, d.m_MyString); } }; void main() { Shallow a, b; Deep c, d; // shallow copy a = b; // this will cause a.m_MyString == b.m_MyString // this will also cause an error when the objects go out of scope // since the pointer will be deleted twice! // deep copy c = d; // this will cause c.m_MyString != d.m_MyString // however, strcpy(c.m_MyString, d.m_MyString) == 0

                L Offline
                L Offline
                led mike
                wrote on last edited by
                #7

                Zac Howland wrote:

                IN GENERAL, it is a bad idea to simply copy the addresses.

                Not sure what your point is. It seemed to me that statement suggests that something is inherently wrong with a "shallow-copy" design. If that is what is intended then I disagree.

                Zac Howland wrote:

                Not CString,

                Still not sure on that but you can find all sorts of reference to CString implementing copy-on-write on the net. Also the VC6 Strcore.cpp has a CString member function called "CopyBeforeWrite" that is called in many places like MakeUpper()/Lower() and MakeReverse() to name a few. So at some point it seems someone at least thought CString used a copy-on-write design. :)

                "What classes are you using ? You shouldn't call stuff if you have no idea what it does"
                Christian Graus in the C# forum

                led mike

                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