Copy constructor
Defined:
Only a single parameter, which is a reference to the object of this class type (commonly used const adornments), is a copy constructor. The copy constructor can be used to:
1. Display or implicitly initialize an object based on another object of the same type
2. Copy an object and pass it as an argument to a function
3. Copy an object from function return
4. Initialize the elements in the sequential container
5. Initialize array elements based on element initialization list
--The above definition is from "C + + Primer Chinese version 4th edition"
Shallow copy/Shallow copy
In the first article, if a custom class object has been initialized and used to initialize another object of the same type, assuming that there is a pointer-type variable in the class, and if no constructed copy constructor is displayed, then the compiler automatically generates a copy constructor, at which point the copy is called a shallow copy.
a more accurate definition is as follows:
In C + +, when you initialize another object with one object, only the members are copied, and no resources are copied, so that two objects point at the same time to the replication of the same resource, called shallow copy.
So what is the risk of a shallow copy?
If a pointer member function exists in a custom class, then:
1, shallow copy just copy the pointer, so that 2 pointers to the same address, so that when the object block end Call function destruction, it will cause the same resource 2 destruction, that is, delete 2 times, causing the program crashes.
2. A shallow copy causes 2 pointer members to point to the same memory address, modifying the value pointed to by one of the pointers, changing the value pointed to by the pointer of another object
3. When the memory is released, the class passed as the argument fails because of the failure of the destructor, causing the memory leak.
Above from http://blog.csdn.net/feitianxuxue/article/details/9275979
To react by an instance:
Code:
#include <iostream>using namespace STD;classa{ Public: A () {data =New Char;cout<< data << Endl;cout<<"default constructor"<< Endl; } ~a () {cout<<"destructor"<< Endl;DeleteDatacout<< data << Endl; data = NULL; }Private:Char* DATA;};intMainintargcChar* argv[]) {a A; A B (a);return 0;}
For the above code, a single step, statement-wise debugging:
1, A a;
set breakpoints at the place, take the step-by-step F11 debugging, enter into the custom constructor:
2, for the object a--dynamic allocation of a memory to data, at this time the value of data is 0X004084F8, storage data This is worth the memory address of 0X002FFBF4:
3, continue to execute F11, you will find that the program will not enter the custom constructor again, because A b(a)
the copy constructor executed at this time, because the class does not display the definition, it is done automatically by the compiler, the program runs to:
4, before the end of the main function, because the declaration period of the object has arrived, so call the destructor, and the order of the destructor is a backward-forward destructor, that is, object B after object A is defined, then B in a before a destructor. It is also possible to judge the destructor order by observing that the address of the stored data variable is 0x002ffbe8 and the address of data stored in a is different.
But the data value in a, B is 0x004084f8
5. After the delete frees the memory resource, the value of data becomes the character in the 0x004084f8< string cannot ...;: ( personal Understanding, the characters in the 0x004084f8< string here cannot be ...> with 0x004084f8< ...> There is an essential difference, the latter memory address corresponds to an actual memory space, memory stored in the data display garbled. While the former appears to be a memory address value, but does not correspond to an actual memory space, like a school number before it can correspond to a student, but after the student information is written off, although the school number exists, but can not find this person. if the understanding is wrong, please correct, humbly learn ~) , at this time data becomes a hanging pointer.
6. To avoid hanging the pointer, point the pointer to null:
7. End of First destruction:
8, the 2nd time the destruction, namely object A's destruction. At this point, it is observed that the value stored in data has changed to a character in the 0x004084f8< string that cannot be ...>, that is, data is a wild pointer.
9, continue to refactor, causing the program to crash:
The above debugs explain the possible impact of a shallow copy:
1, the same memory space destruction 2 times caused the program crashes
2, one class member modifies the resource, will make the other also change with it
3, 3rd in doubt, data points to the memory stored in the heap, but has been released by Object B, and the data is a local variable, its storage address in the stack, the program ends, the system automatically recover the resources in the stack, then the so-called memory leak is what part of the leaked memory resources it??? I don't understand, maybe I understand the mistake???
Deep copy/Deep copy
Defined:
When a copy object has a reference to another resource (such as a heap, file, system, and so on), the object opens up a new resource instead of simply assigning a pointer or reference to the resource in the copied object. simply put, the non-shared block of memory resources, instead, re-open a piece of content, copy the data into the newly opened memory.
To react by an instance:
#include <iostream>using namespace STD;classa{ Public: A () {data =New Char;cout<< data << Endl;cout<<"default constructor"<< Endl; AConsta& a) {data =New Char;memcpy(Data, A.data,sizeof(A.data)); } ~a () {cout<<"destructor"<< Endl;DeleteDatacout<< data << Endl; data = NULL; }Private:Char* DATA;};intMainintargcChar* argv[]) {a A; A B (a);return 0;}
With single-Step debugging F11:
1, the program execution to object A's instantiation:
2. Enter the constructor of object A:
3, at this time in the heap for data to open up a memory, memory address is 0x005584f8
4, notice, in shallow copy, the program runs directly to return 0;
. While deep copy, F11 after entering the copy constructor:
5. Observe that the memory address that data points to is 0x00558d10 and a.data (0X005584F8) is different. The purpose of memcpy is to replicate data.
6. Perform the destructor of Object B, at which point the value of data is 0X00558D10, which is exactly the address of the memory space allocated in the B object replication construct.
7. The resources in the heap occupied by object B are freed.
8, the 2nd time of destruction, that is, the release of the resource in a object, at this time the value of data is 0x005584f8
9, the program does not appear the situation of collapse, indicating that 2 times the destruction of the success.
So, when do you use a shallow copy? When do you use a deep copy?
It is best to use a deep copy or smart pointer in http://www.cricode.com/753.html. A deep copy takes up additional memory resources relative to a shallow copy, but is more secure to use.
Note 10: Copy constructor, deep copy, shallow copy