C ++ shallow vs. deep copy distinction

  Meeting yesterday simplex talked about programming specification, where a is the class contains a pointer type need to implement the assignment operator and copy constructor, what is in the copy constructor, before first look deep copy and shallow copy the difference. First consider the case of a known object is copied, the compiler will automatically call one of the constructor - copy constructor, if the user is not defined copy constructor will call the default copy constructor.

Look at an example, there is a class of students, members of the student population data and names:

 

#include <the iostream>   

the using namespace STD; 

 

class Student 

{ 

Private: 

	int NUM; 

	char * name; 

public: 

	Student (); 

	~ Student (); 

}; 

 

Student :: Student () 

{ 

	name = new new char (20 is); 

	COUT << "Student" << endl; 

 

} 

Student Student :: ~ () 

{ 

	COUT << "Student ~" << (int) name << endl; 

	Delete name; 

	name = NULL; 

} 

 

int main () 

{ 

	{/ / let s1 and s2 braces into partial objects convenient test 

		Student s1; 

		Student s2 (s1); // copy target 

	} 

	System ( "PAUSE"); 

	return 0; 

}

  

The results: a call to the constructor, destructor called twice, members of the two objects pointer within the meaning of the same memory, which can cause the problem? name pointer is assigned a memory, but at the end of the program memory was freed twice, Crash!


This is due to the build system when we do not have their own definition of the copy constructor, the default copy constructor will be called when copying object, is shallow copy! I.e., the pointer will appear after the name of the two copies of the same pointer to a memory space.

Therefore, when the object containing the pointer members may be copied, you must define your own copy constructor, so that the copy of the object pointer member has its own memory space, i.e. deep copy, thus avoiding the memory leak.
Examples add copy constructor own definition:

 

#include <iostream>  

using namespace std;

 

class Student

{

private:

	int num;

	char *name;

public:

	Student();

	~Student();

	Student(const Student &s);//拷贝构造函数,const防止对象被改变

};

 

Student::Student()

{

	name = new char(20);

	cout << "Student" << endl;

 

}

Student::~Student()

{

	cout << "~Student " << (int)name << endl;

	delete name;

	name = NULL;

}

Student::Student(const Student &s)

{

	name = new char(20);

	memcpy(name, s.name, strlen(s.name));

	cout << "copy Student" << endl;

}

 

main int () 

{ 

	{// make braces s1 and s2 into partial objects convenient test 

		Student s1; 

		Student s2 (s1); // copy target 

	} 

	System ( "PAUSE"); 

	return 0; 

}

  

Results of: calling a constructor, a custom copy constructor, destructor twice. Pointer members referred to two different memory objects.
Summary: shallow copy just copy the pointer, two pointers pointing to the copy of the same memory space, only a deep copy pointer is copied, and the content of the pointer to be copied, the pointer is pointing to the deep copy two different addresses pointer.
Say a few words:
When the pointer member is present in the object, in addition to copying the object need to be considered when custom copy constructor, should also consider the following two situations:
1. When the parameter is a function of the object argument to the parameter It is actually a copy of the object argument, either automatically by the system copy constructor;
2. when the function returns a value of an object, the object is actually a copy of the function object, a return of the function call. 3. The essence of the problem lies in shallow copy brought destructor frees heap memory several times, using std :: shared_ptr, can be the perfect solution to this problem.

 

Guess you like

Origin www.cnblogs.com/wjcoding/p/10955017.html