Deep and shallow copies

P

pauldepstein

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
 
J

Jim Langston

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;
};
 
F

Frederick Gotham

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.
 
F

Frederick Gotham

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; }
 
C

Clark S. Cox III

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;
}
};
 
A

AnonMail2005

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top