Deep and Shallow Copying

This is a discussion on Deep and Shallow Copying within the C++ Programming forums, part of the General Programming Boards category; Hi i need to have a understanding of Connecting 2 strings using binary “+” operator in plain English. i don’t ...

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Deep and Shallow Copying

    Hi

    i need to have a understanding of Connecting 2 strings using binary “+” operator in plain English. i don’t need to be able to write any code just need to understand the principle.

    I believe i have a basic understanding on how deep copying works

    · Measure lastname
    · Measure firstname
    · Create a block of memory for firstname + lastname + 1 for string marker
    · Copy firstname into new block
    · Concatenate lastname into new name block
    · Delete firstname (if this is not done a memory leak will occur)
    · Return new name

    this will be using pointers as its a deep copy. if any of the above is incorrect could you point me in the right direction

    also if possible can anyone help me understand how this process above of copying a 2 strings together is different for a shallow copy?

    Thanks

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    When people talk about deep and shallow copies, they are talking about copy constructors (not + operator)?

    And I think you shouldn't delete first name, because you received it as a parameter and didn't allocate it.

    However, you simply can't overload operators for built-in types (two char*-s), so unless you are building your own string class the question is rather vague.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    Quote Originally Posted by peckitt99 View Post
    Hi

    i need to have a understanding of Connecting 2 strings using binary “+” operator in plain English. i don’t need to be able to write any code just need to understand the principle.

    I believe i have a basic understanding on how deep copying works

    · Measure lastname
    · Measure firstname
    · Create a block of memory for firstname + lastname + 1 for string marker
    · Copy firstname into new block
    · Concatenate lastname into new name block
    · Delete firstname (if this is not done a memory leak will occur)
    · Return new name

    this will be using pointers as its a deep copy. if any of the above is incorrect could you point me in the right direction

    also if possible can anyone help me understand how this process above of copying a 2 strings together is different for a shallow copy?

    Thanks
    The concatenation of two strings looks like this:

    Code:
    MyStringType firstName=MyStringType ("Jane"), lastName=MyStringType("Doe"), fullName;
    fullName= firstName + lastName;
    Where firstName and lastName do not change as a result of the operation, but the return value is the concatenation of the two strings.

    So you should not delete either of the two strings passed to operator+. Otherwise everything else is correct.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Still has nothing to do with deep or shallow copying, though.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Here's an example that shows the concept of Shallow vs. Deep copying:
    Code:
    class Something
    {
    private:
       char*  m_SomeData;
       char*  m_ArrayOfStrings[10];
    
    public:
       void ShallowCopy( const Something* pData )
       {
          // Just copy the pointers.
          m_SomeData = pData->m_SomeData;
          m_ArrayOfStrings = pData->m_ArrayOfStrings;
       }
    
       void DeepCopy( const Something* pData )
       {
          // Copy the data that the pointers are referencing.
          m_SomeData = new char[ strlen( pData->m_SomeData ) ];
          strcpy( m_SomeData, pData->m_SomeData );
    
          for ( int i = 0; i < 10; ++i )
          {
             m_ArrayOfStrings[ i ] = new char[ strlen( pData->m_ArrayOfStrings[ i ] ) ];
             strcpy( m_ArrayOfStrings[ i ], pData->m_ArrayOfStrings[ i ] );
          }
       }
    
       ...
    };
    Shallow copying makes an exact byte by byte copy (including the pointers & references) of the other class.
    Deep copying doesn't just copy the pointer values, it allocates new memory and copies the data that is being pointed to, not just the address of the data...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deep and Shallow Copy
    By peckitt99 in forum C++ Programming
    Replies: 2
    Last Post: 05-13-2007, 07:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21