Deep and shallow copies

Discussion in 'C++' started by pauldepstein@att.net, Oct 22, 2006.

  1. Guest

    I recently had a job interview question which I totally failed. (The
    question seemed excellent from an objective point of view, but having
    completely failed to do it, my subjective feelings are different.)

    It's hard for me to recall the question exactly. But basically, it
    involved a class construction with a shallow copy constructor.

    The question was basically "What is the bug in this code?" The
    intended answer was that shallow copies would lead to an attempt to
    destruct the same object twice, and that a deep copy constructor was
    needed. The intended answer involved rewriting the shallow copy
    constructor.

    Because I think technical interviews should be a learning experience, I
    tried to find out about these concepts through googling. However, when
    I researched copy constructors, I mainly found shallow copy
    constructors that the examiner said were inappropriate for the example.

    Can anyone show me where to get guidance on these concepts? Can anyone
    think of an example where shallow copies interfere with the destructor,
    and where deep copies are therefore needed.

    I'm sorry that I can't recall the question more accurately but I hope
    I've said enough for people to offer words of wisdom (perhaps by
    recommending other websites.)

    Thanks a lot for your help.

    Paul Epstein
    , Oct 22, 2006
    #1
    1. Advertising

  2. Jim Langston Guest

    <> wrote in message
    news:...
    >I recently had a job interview question which I totally failed. (The
    > question seemed excellent from an objective point of view, but having
    > completely failed to do it, my subjective feelings are different.)
    >
    > It's hard for me to recall the question exactly. But basically, it
    > involved a class construction with a shallow copy constructor.
    >
    > The question was basically "What is the bug in this code?" The
    > intended answer was that shallow copies would lead to an attempt to
    > destruct the same object twice, and that a deep copy constructor was
    > needed. The intended answer involved rewriting the shallow copy
    > constructor.
    >
    > Because I think technical interviews should be a learning experience, I
    > tried to find out about these concepts through googling. However, when
    > I researched copy constructors, I mainly found shallow copy
    > constructors that the examiner said were inappropriate for the example.
    >
    > Can anyone show me where to get guidance on these concepts? Can anyone
    > think of an example where shallow copies interfere with the destructor,
    > and where deep copies are therefore needed.
    >
    > I'm sorry that I can't recall the question more accurately but I hope
    > I've said enough for people to offer words of wisdom (perhaps by
    > recommending other websites.)
    >
    > Thanks a lot for your help.
    >
    > Paul Epstein


    You mean this?

    class MyClass
    {
    public:
    MyClass( int Size ) { buffer = new char[Size]; };
    ~MyClass() { delete[] buffer; }
    private:
    char* buffer;
    };
    Jim Langston, Oct 22, 2006
    #2
    1. Advertising

  3. Paul posted:

    > The question was basically "What is the bug in this code?" The
    > intended answer was that shallow copies would lead to an attempt to
    > destruct the same object twice, and that a deep copy constructor was
    > needed. The intended answer involved rewriting the shallow copy
    > constructor.
    >
    > Because I think technical interviews should be a learning experience, I
    > tried to find out about these concepts through googling. However, when
    > I researched copy constructors, I mainly found shallow copy
    > constructors that the examiner said were inappropriate for the example.
    >
    > Can anyone show me where to get guidance on these concepts? Can anyone
    > think of an example where shallow copies interfere with the destructor,
    > and where deep copies are therefore needed.



    Here's an example of where shallow-copy will suffice:

    class NumberAccessor {
    private:

    int i;

    public:

    NumberAccessor() : i(0) {}

    void Increment() { ++i; }

    void Decrement() { --i; }

    /* NumberAccessor(NumberAccessor const&) {} Implicitly defined */
    };

    And here's an example of where deep-copy is required:

    class NumberAccessor {
    private:

    int *const p;

    public:

    NumberAccessor() : p(new int(0)) {}

    void Increment() { ++*p; }

    void Decrement() { --*p; }

    NumberAccessor(NumberAccessor const&) : p(new int(0)) {}
    };


    Rule of thumb is as follows:

    If the constructor acquires resources (such as dynamically allocated
    memory), then it needs a copy-constructor which will acquire new resources
    for the new object, rather than still use the resources associated with the
    original object.

    --

    Frederick Gotham
    Frederick Gotham, Oct 22, 2006
    #3
  4. Frederick Gotham posted:

    > /* NumberAccessor(NumberAccessor const&) {} Implicitly defined */



    My brain isn't working well at the moment. That should have been something
    like:

    /* NumberAccessor(NumberAccessor const &original) : i(original.i) {} */
    /* Implicitly Defined */


    > NumberAccessor() : p(new int(0)) {}



    I should have a destructor to go with that:

    ~NumberAccessor() { delete p; }

    --

    Frederick Gotham
    Frederick Gotham, Oct 22, 2006
    #4
  5. wrote:
    > I recently had a job interview question which I totally failed. (The
    > question seemed excellent from an objective point of view, but having
    > completely failed to do it, my subjective feelings are different.)
    >
    > It's hard for me to recall the question exactly. But basically, it
    > involved a class construction with a shallow copy constructor.
    >
    > The question was basically "What is the bug in this code?" The
    > intended answer was that shallow copies would lead to an attempt to
    > destruct the same object twice, and that a deep copy constructor was
    > needed. The intended answer involved rewriting the shallow copy
    > constructor.
    >
    > Because I think technical interviews should be a learning experience, I
    > tried to find out about these concepts through googling. However, when
    > I researched copy constructors, I mainly found shallow copy
    > constructors that the examiner said were inappropriate for the example.
    >
    > Can anyone show me where to get guidance on these concepts? Can anyone
    > think of an example where shallow copies interfere with the destructor,
    > and where deep copies are therefore needed.


    Think about what happens in the compiler-provided copy-constructor and
    assignment operator in the following:

    class BadString
    {
    char *m_buffer;
    public:
    BadString()
    : m_buffer(new char[100])
    {
    }

    ~BadString()
    {
    delete [] m_buffer;
    }
    };



    --
    Clark S. Cox III
    Clark S. Cox III, Oct 22, 2006
    #5
  6. Guest

    wrote:
    > I recently had a job interview question which I totally failed. (The
    > question seemed excellent from an objective point of view, but having
    > completely failed to do it, my subjective feelings are different.)
    >
    > It's hard for me to recall the question exactly. But basically, it
    > involved a class construction with a shallow copy constructor.
    >
    > The question was basically "What is the bug in this code?" The
    > intended answer was that shallow copies would lead to an attempt to
    > destruct the same object twice, and that a deep copy constructor was
    > needed. The intended answer involved rewriting the shallow copy
    > constructor.
    >
    > Because I think technical interviews should be a learning experience, I
    > tried to find out about these concepts through googling. However, when
    > I researched copy constructors, I mainly found shallow copy
    > constructors that the examiner said were inappropriate for the example.
    >
    > Can anyone show me where to get guidance on these concepts? Can anyone
    > think of an example where shallow copies interfere with the destructor,
    > and where deep copies are therefore needed.
    >
    > I'm sorry that I can't recall the question more accurately but I hope
    > I've said enough for people to offer words of wisdom (perhaps by
    > recommending other websites.)
    >
    > Thanks a lot for your help.
    >
    > Paul Epstein

    Normally, whenever you have a raw pointer in your class you need to
    explicity write the constructor, destructor, assignment operator,
    and copy constructor to deal with the pointer. Otherwise you get
    this double delete issue of having only the pointer (not what it
    points to) being copied.

    This must be in the FAQ but I couldn't find it at the moment.

    You should get a copy of Effective C++ by Scott Meyers (also
    mentioned in the FAQ). He distills this type of essential
    knowledge into easy to understand examples.
    , Oct 23, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Johansson

    deep and shallow copy

    Tony Johansson, May 19, 2005, in forum: C++
    Replies:
    5
    Views:
    9,813
    Rapscallion
    May 19, 2005
  2. Replies:
    1
    Views:
    528
    Victor Bazarov
    Jun 24, 2005
  3. Replies:
    26
    Views:
    2,083
    Roland Pibinger
    Sep 1, 2006
  4. PerlFAQ Server
    Replies:
    0
    Views:
    113
    PerlFAQ Server
    Jan 17, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    94
    PerlFAQ Server
    Apr 13, 2011
Loading...

Share This Page