CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2003
    Location
    mumbai
    Posts
    18

    Smile deep and shallow copy

    How deep and shallo copy works internally in copy of class data.
    If i use copy constuctor then all data is copied by deep concept and when I use "=" equal to operator data is copied by shallow concept. what exactly is the differance between these two concepts.

    Sanjiv
    sanjiv

  2. #2
    Join Date
    Dec 2002
    Posts
    126
    As I understand it, a shallow copy only copies the data members itself. Lets say you have a class that contains a single pointer, pString, to an array of characters. In a shallow copy, Copy.pString == Original.pString - the pointer is simply assigned the same value. In a deep copy, new memory is allocated for Copy.pString, and the data in Original.pString copied over to it. Copy.pString and Original.pString point to different locations.

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    By default the copy constructor and assignment operator are making so-called shallow copies of the object meaning that all of the member values will be copied. This works well if the members are values, but causes problems for members which actually points to dynamically allocated memory. The pointer will be copied - but not the memory it points to...this will result in having both members pointing to the same dynamically allocated memory.

    Therefore, if you have a class with dynamic memory allocation a deep copy is required to guarantee that all members gets copied properly. To make a deep copy, you must write a copy constructor and overload the assignment operator.
    Code:
    class CFoo()
    {
    public:
      // Constructor
      CFoo() { m_pArray = new char[100]; }
    
      // Destructor
      ~CFoo() { delete [] m_pArray; }
    
      // Copy constructor
      CFoo(const CFoo &refcSource)
      {
        // Allocate new space
        m_pArray = new char[strlen(refcSource.m_pArray) + 1];
    
        // Copy values
        strcpy(m_pArray, refcSource.m_pArray);
      }
    
      // Assignment operator
      CFoo& operator=(const CFoo &refcSource)
      {
        // Check whether it is the same instance
        if(refcSource != this)
        { 
          // Release old memory
          delete [] m_pArray;
    
          // Allocate new space
          m_pArray = new char[strlen(refcSource.m_pArray) + 1];
    
          // Copy values
          strcpy(m_pArray, refcSource.m_pArray);
        }
    
        return *this;
      }
    
    private:
      char *m_pArray;
    };

  4. #4
    Join Date
    Sep 2002
    Posts
    1,747
    Note also that the distinction between copies applies to any resource sharing, not just pointers. When you program for various operating systems, you will find that the API exposes its resources to you in various formats (like the various forms of handles in some OSes). You must decide whether it is appropriate when your class is copied whether you desire only the resource identifier gets copied, or whether it is appropriate to create a whole new resource (as dtors may want to clean up the resource).
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Can I also point out that copying as a concept often does not apply to resource-owning classes, so one important consideration in the design phase is whether to disable copying or not.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  6. #6
    Join Date
    Apr 2003
    Location
    mumbai
    Posts
    18

    Thanks

    Thanks to all for their reply.
    sanjiv

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

Featured